Wild Goat
Wild Goat

Reputation: 3579

Mongo db query nested document

I am trying to come up with a mongo query which would return me document and only fields (changes in nested document) for particular time range.

This is what I've tried but unfortunately that doesn't work.

Thanks for any help!

Query:

   db.keywords.find(
        {
            $and :[{"_id" : "5374c7a7ac58b0d3b5e970fa"},{"field" : "keywordId"},{"adwordsChanges.timestamp" : {$gte:1400162491325, $lte:23918779329}}]
        })

Document:

{
   {
    "_id": ObjectId("5374c7a7ac58b0d3b5e970fa"),
    "documentchanges": [{
        "timestamp": NumberLong("1400162491325"),
        "field": "syncState",
        "old": null,
        "new": "OK"
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "keywordId",
        "old": null,
        "new": NumberLong("23918779329")
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "adGroupId",
        "old": null,
        "new": NumberLong("16972286472")
        }]
    }, {
    "_id": ObjectId("5374c7a7ac58b0d3b5e970f9"),
    "documentchanges": [{
        "timestamp": NumberLong("1400162491325"),
        "field": "syncState",
        "old": null,
        "new": "OK"
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "keywordId",
        "old": null,
        "new": NumberLong("23918779329")
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "adGroupId",
        "old": null,
        "new": NumberLong("16972286472")
        }]
    }
}

Upvotes: 0

Views: 351

Answers (1)

Simulant
Simulant

Reputation: 20132

Your _id find is looking for a string but your object contrains an ObjectId. Use this:

{"_id" : ObjectId("5374c7a7ac58b0d3b5e970fa")}

you are missing a . operator before field. use:

{"documentchanges.field" : "keywordId"}

There is also no tag adwordsChanges in your top level document, but you are reffering it here:

{"adwordsChanges.timestamp" : {$gte:1400162491325, $lte:23918779329}}

change this to documentchanges as well:

{"documentchanges.timestamp" : {$gte:1400162491325, $lte:23918779329}}

Also there is no timestamp lesser than or equal to 23918779329. Change the number.

Upvotes: 2

Related Questions