I have a document that looks something like this:
{ "_id" : ObjectId("4e5044ecf9d7954533000002"), "events" : [
{
"state" : 1,
"time" : 1313882989,
},
{
"time" : 1313883005,
"state" : 0,
开发者_开发百科 }
]}
You can see that "events" is an array of embedded documents. state:1 means that the document was set to "active" at that time (seconds in UTC), and state:0 means that it was set "inactive" at that time. This essentially give me a time range of when this document was active (from 1313882989 to 1313883005)
Now Im trying to determine what documents were active at any particular time, like at 1313883013, a time that falls between the active and inactive events.
I can get something with this query:
db.plays.find({events:{$elemMatch:{state:1,time:{$lte:1313883013}}}})
It matches a complete embedded document where state=1 AND time<1313883013. This is not enough however, because I need to ensure that the document also does not contain an inactive event at or before 1313883013. So I've tried this:
db.plays.find({events:{$elemMatch:{state:1,time:{$lte:1313883013}},$not:{$elemMatch:{state:0,time:{$lte:1313883013}}}}})
but to no avail. And I've tried different variations of that as well.
I hope I've made this clear enough, thanks in advance for any help!!
Just don't include state:1 in your query.
From the result you get, check the value of state (0 or 1).
精彩评论