C# Driver Collection GetStats Error (Duplicate element name 'ns'.)

546 views
Skip to first unread message

Kader Belbina

unread,
Aug 3, 2011, 8:21:32 PM8/3/11
to mongodb-user
I am trying to periodically (nightly) get a bunch of statistics for
each of my collections and commit them to a database. Here is my code:

public IDictionary<string , CollectionStatsResult>
GetCollectionStats()
{
MongoServer server =
MongoServer.Create(DbConstants.GetMongoDbConnectionString());
MongoDatabase mongoDatabase =
server.GetDatabase(DatabaseName);

var collections = mongoDatabase.GetCollectionNames();
IDictionary<string , CollectionStatsResult> results = new
Dictionary<string, CollectionStatsResult>();
foreach (var collectionName in collections)
{
if (collectionName.Equals("systems.index")) continue;

var collection =
mongoDatabase.GetCollection(collectionName);
var stats = collection.GetStats(); // This line fails
results.Add(collectionName, stats);
}

return results;
}



The collection.GetStats() line is throwing an exception:
InvalidOperationException
Duplicate element name 'ns'

I am using 1.1 of the C# driver and 1.82 of MongoDb.

Robert Stam

unread,
Aug 3, 2011, 8:53:52 PM8/3/11
to mongodb-user
Can you find out which collection name is failing (I assume it's not
failing on all of them) and then do this from the mongo shell:

> db.failingcollectionname.stats() // replace failingcollectionname with the name of the collection that is failing

and report back the output?

Thanks.

Kader Belbina

unread,
Aug 3, 2011, 9:00:04 PM8/3/11
to mongodb-user
It fails on the first one "coverage"

db.coverage.stats()

Response from server:
{
"retval": {
"ns": "production.coverage",
"count": 11078,
"size": 310192,
"avgObjSize": 28.000722152013,
"storageSize": 696320,
"numExtents": 4,
"nindexes": 1,
"lastExtentSize": 524288,
"paddingFactor": 1,
"flags": 1,
"totalIndexSize": 532480,
"indexSizes": {
"_id_": 532480
},
"ok": 1
},
"ok": 1

Robert Stam

unread,
Aug 3, 2011, 9:11:03 PM8/3/11
to mongodb-user
I'm perplexed by your output. When I run that command it looks like
this for me:

> db.test.stats()
{
"ns" : "test.test",
"count" : 1,
"size" : 36,
"avgObjSize" : 36,
"storageSize" : 2304,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 2304,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8192,
"indexSizes" : {
"_id_" : 8192
},
"ok" : 1
}
>

Not sure where you got your "retval" from.

Kader Belbina

unread,
Aug 3, 2011, 9:12:49 PM8/3/11
to mongodb-user
Ah okay sorry should've said but I executed that command thru Rock
Mongo.

Robert Stam

unread,
Aug 3, 2011, 9:34:06 PM8/3/11
to mongodb-user
I was expecting to see a duplicate "ns" element in your output, as
that would have explained the exception.

I suppose the next step is to ask for a stack trace.

Kader Belbina

unread,
Aug 3, 2011, 9:43:03 PM8/3/11
to mongodb-user
at MongoDB.Bson.BsonDocument.Add(BsonElement element)
at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
Type nominalType, IBsonSerializationOptions options)
at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader
bsonReader, Type nominalType, IBsonSerializationOptions options)
at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer
buffer, IBsonSerializationOptions serializationOptions)
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument]
(BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions
serializationOptions)
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection
connection, MongoRequestMessage message)
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst()
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1
source)

Robert Stam

unread,
Aug 3, 2011, 10:04:13 PM8/3/11
to mongodb-user
That stack trace seems incomplete... what's below FirstOrDefault?

Robert Stam

unread,
Aug 3, 2011, 10:10:36 PM8/3/11
to mongodb-user
In any case, it seems like the server is returning a duplicate "ns"
element in the reply, but it's hard to prove that (other than that is
what the exception says).

It's possible that going through RockMongo is hiding the duplicate
"ns" element. Can you try the stats function directly in the mongo
shell? And while you are at it, can we verify the server version?

> db.version()

Robert Stam

unread,
Aug 3, 2011, 10:13:02 PM8/3/11
to mongodb-user
FYI, code very similar to yours runs in my environment without
throwing that exception. This code:

foreach (var collectionName in database.GetCollectionNames()) {
var collection = database[collectionName];
var stats = collection.GetStats();
Console.WriteLine("{0}: {1} objects", collectionName,
stats.ObjectCount);
}

results in this output:

animals: 1 objects
categories: 10000002 objects
fs.chunks: 1 objects
fs.files: 1 objects
magicdiscriminator: 1 objects
MyColl: 2 objects
system.indexes: 17 objects
system.profile: 32 objects
test: 1 objects
test.foo: 1 objects
testuserdata: 2 objects
testusersettings: 2 objects
userdata: 2 objects
users: 1 objects
usersettings: 2 objects
uuids: 1 objects

and no exceptions.

Kader Belbina

unread,
Aug 3, 2011, 10:52:20 PM8/3/11
to mongodb-user
Could it have anything to do with being in a sharded enivornment?

Robert Stam

unread,
Aug 3, 2011, 11:24:39 PM8/3/11
to mongodb-user
It could be.

If you get a change to run db.coverage.stats() directly in the mongo
shell instead of RockMongo let me know if it shows a duplicate "ns"
element.

Robert Stam

unread,
Aug 5, 2011, 1:21:14 PM8/5/11
to mongodb-user
I ran a test against a sharded environment and found that it is in
fact returning a duplicate "ns" element, which would explain your
exception.

I created a JIRA ticket for this:

https://jira.mongodb.org/browse/SERVER-3539
Reply all
Reply to author
Forward
0 new messages