1

How can we write query in mongodb which is equals to the inner select sql query.

select field1, field2 
from workflowTable 
where id in(select idField from usersTable)

No of hits to the db should be same.

2 Answers 2

2

Let's think you have 3 documents.collection name is users.

   {
    "_id": ObjectId("4efa8d2b7d284dad101e4bc9"),
    "Last Name": "DUMONT",
    "First Name": "Jean",
    "alert":1,
    "Date of Birth": "01-22-1963"
},
{
    "_id": ObjectId("4efa8d2b7d284dad101e4bc7"),
    "Last Name": "PELLERIN",
    "First Name": "Franck",
    "alert":2,
    "Date of Birth": "09-19-1983",
    "Address": "1 chemin des Loges",
    "City": "VERSAILLES"
},
{
    "_id": ObjectId("4efa8d2b7d284dad101e4bc7"),
    "Last Name": "PELLERIN",
    "First Name": "Franck",
    "alert":3,
    "Date of Birth": "09-19-1983",
    "Address": {
        "Street": "1 chemin des Loges",
        "City": "VERSAILLES"
    }
}

In above document i want only 2 document which column contain alert 1 and 2.Query for that

db.users.find({alert:{$in:[1,2]}})

This will give me first two document.

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

Comments

0

You can use $in parameter in mongodb for this query. Check this tutorial for more information.

Comments

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.