Tuples in Mongo document with same key?

1,176 views
Skip to first unread message

Russell Bateman

unread,
Aug 22, 2012, 7:50:23 PM8/22/12
to mongodb-user
Is there no way in Mongo to get a document that looks like the one I'm trying to insert here?
> db.subs.insert( { "x":9, "tuple":"value", "tuple":"value", "tuple":"different value" } );

> db.subs.findOne();
{
    "_id" : ObjectId("50356db633f95e308785e7d4"),
    "x" : 9,
    "tuple" : "different value"
}

> db.subs.insert( { "x":9, "data": { "tuple":"value", "tuple":"value", "tuple":"different value" } } );

> db.subs.findOne();
{
    "_id" : ObjectId("50356db633f95e308785e7d5"),
    "x" : 9,
    "data" : {
        "tuple" : "different value"
    }
}

Note that the first two tuples were ignored/dropped/overwritten, and not just because they had the same value. I'm not totally surprised by this, but I wondered. Certainly it's the case in the Java driver, in BasicDBObject, that stuff is done in a Map which doesn't allow multiple identical keys so it's doubtful I could get anything back anyway.

Would someone like to growl at me or discuss this? If it worked, I could make use of it and in fact had been blithely writing code as if it worked until a little while later noticing that it did not.

Many thanks.

Kristina Chodorow

unread,
Aug 23, 2012, 10:22:58 AM8/23/12
to mongod...@googlegroups.com
BSON doesn't specifically disallow it, but most languages don't support it.  C++ and C do, but languages that implement documents as hashes can't.  Even if you saved a doc with duplicate keys in C/C++, you wouldn't be able to see them in the shell.  And querying for it would be kind of wacky.  So, generally don't do that.

Is there a reason you can't do tuple:["value", "value", "different value"]?

Russell Bateman

unread,
Aug 23, 2012, 10:34:08 AM8/23/12
to mongod...@googlegroups.com
Thank you for responding. I think I just wanted confirmation--to make certain that I wasn't missing out on something that could be (appropriately) done. I have indeed decided to use an alternate representation, possibly the one you suggest or maybe just a space-delimited string (since the values I really want are identifiers with no spaces or punctuation rather than what my example showed).

Thanks, Kristina.

Russ
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Reply all
Reply to author
Forward
0 new messages