简体   繁体   中英

Querying documents in mongoose

Consider the following sample document

[{
"Name" : "John doe",
"age"  : 22,
"email" : "john@doe.com"
},
{
"Name" : "William",
"age"  : 28,
"email" : "william@william.com"
},
{
"Name" : "jack",
"age"  : 22,
"email" : "jack@jack.com"
}]

I have an array of objects with me with the following structure
[{'name':'jack','age':10},{'name':'john','age':20}]
How do I perform a query in the document in a way such that I can match the name field and age field of my array of objects of each element. Basically, the name jack and the age 10 should query with the sample and after that the name john and the age 20 should perform the query.
I know that $elemMatch could have been used if it was the other way around where I had to query with single elements in an array. Is there any way of doing this?

If you are not talking about searching the retrieved documents. You can search database like this:

    var searchArr = [{name:'jack',age:10},{name:'john', age:20}]

    searchArr.forEach(function(element){
        var query = Model.find(element)

        query.then(function(result){
            console.log(result)
        })
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM