ttback
ttback

Reputation: 2111

Unable to query embedded document in Mongodb

I have been studying how to use Mongodb, and have been stuck at querying data in embedded document for hours. I found example on Mongodb's website here: http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-EmbeddingandLinking

However, when I tried it with my own data such as

db.test.save({"name":"record", "items":[{"histPrices":[10,12],"name":"stuff"}]})
db.test.find({items.name:"stuff"})

It never finds anything, instead it is given me an error: Fri Nov 2 14:24:09 SyntaxError: missing : after property id (shell):1

Anyone has any tips on this?

Upvotes: 0

Views: 246

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

I'm not sure why you are using histPrices.name. But if you're using dot-notation, you should enclose the complex name in quotes.

> db.test.find({'items.name':"stuff"})
{
    "_id" : ObjectId("509416a3d78ed5021c34c12d"),
    "name" : "record",
    "items" : [
        {
            "histPrices" : [
                10,
                12
            ],
            "name" : "stuff"
        }
    ]
}

Upvotes: 1

Related Questions