0

I am facing issue in lookup in MongoDB query which is stored and called using pymongo3.3. There is an ID column in lookup collection which is dictionary. I am creating this LookupID column in project, but the project is changing the order of dictionary which in turn fails the lookup. . MongoDB version is 3.2 and Python2.7.

I tried useDefaultSort:false option as given [here] https://github.com/strongloop/loopback-connector-mongodb/pull/177, and also tried inserting the record using ordereddictionary through Pymongo but it did not work

Below is the scenario explained with example:

    Collection1 (collection where query is stored)
    {
        "_id":"test",
        "Query": [{
                "$project": {
            "col1": 1,
            "ID": {
                "first": 1,
                "second": 1,
                "third": 1
            }
        }
    },
    {
        '$lookup': {
            'foreignField': 'ID',
            'as': 'LookupOutput',
            'from': 'CollectionLKP',
            'localField': 'ID'
        }
    }
]

}

Calling the query in Python2.7 using Pymongo 3.3

    a = db.Collection1.find_one({"_id":"test"},{"_id":0})
    QueryFilter = a["Query"]

    db.Collection2.aggregate(QueryFilter)

But when I fetch QueryFilter the sequence of ID column changes which fails the lookup. Please help me with possible solution.

    Sample Collection2: 
    {
        "col1": "India",
        "first": "41",
        "second": "secValue",
        "third": "thirdValue"
    }
    Sample CollectionLKP: 
    {
        "_id": "Val1",
        "ID": {
            "first": "41",
            "second": "secValue",
            "third": "thirdValue"
        },
        "colTest": "TestValue"
    }
JJ_R
  • 25
  • 1
  • 1
  • 7

0 Answers0