Thursday, 8 June 2017

Search an item in a collection based on a dynamic key

I am new in NoSQL database and I want to find a way to search all item in the array based on a key, for example : if we have this collection database.collection("col")

{
    "_id" : ObjectId("59389f763007e086bc310c73"),
    "messageIds" : [ 
        1, 
        2, 
        3, 
        4, 
        5
    ],
    "participants" : {
        "59389e953007e086bc310c36" : 0,
        "59389ea63007e086bc310c41" : 0
    },
    "type" : "f"
}

How to find all document that have the participants "59389e953007e086bc310c36" in node.js?

In MongoDb I can find out all items but

db.getCollection('conversations').find(
    {
        "participants.59389e953007e086bc310c36" : 
            {
                $exists: true
            }
    })

I can not do that in Node.js because it will not evaluate user_oid by value

user_oid = "participants."+user_oid;
    let criteria = {
        user_oid: { $exists: true }
    }

    console.log(criteria);
    database.collection(CONVERSATION_COLLECTION).find(criteria, (err,doc) => {
        console.log(doc);
        console.log;

    })



via ADO R

No comments:

Post a Comment