Answer the question
In order to leave comments, you need to log in
MongoDB select from nested array?
Simple question, I can't figure it out. Square brackets are in our Mongo array right?
How can I find the contents of tags (and a specific value in it) using find
db.qtest.insert(
{
date: Date("2013-01-14"),
name: "Roma",
tags: [{Python:14,Ruby:10,C:4}]
}
)
Answer the question
In order to leave comments, you need to log in
Don't try to make tricky queries (which are often slow), it's often better to make the structure of the document where data can be retrieved quickly and easily.
For example, for your task, you can do this:
> db.qtest.insert({
date: Date("2013-01-14"),
name: "Roma",
tags:['python', 'ruby', 'c'],
counts: {python: 14, ruby:10, c:4 }
})
> db.qtest.find({tags: 'python'}, {date: 1, name:1, 'counts.python':1}).pretty()
{
"_id" : ObjectId("55a28f4624db1e49cecbc89f"),
"date" : "Sun Jul 12 2015 21:01:10 GMT+0500 (YEKT)",
"name" : "Roma",
"counts" : {
"python" : 14
}
}
db.qtest.ensureIndex({tags: 1})
, and this query will work quickly when the collection is large.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question