0

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")
        }]
    }
}

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Correct, I fixed that and it works it actually returns results. But what I am actually want to achieve - return only the subdocuments which are in that range. I don't want to see anything else. Is is possible to achieve in mongo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.