Home > database >  How do this query with a local array in mongo?
How do this query with a local array in mongo?

Time:02-03

I have the follow local array, i want to search these registers in my db for show more information, i think in two different solutions but a have problems

var clients = [
{
    "Document_Type": "CC", 
    "Document": "52879473"}, 
{
    "Document_Type": "CC", 
    "Document": "19294395"}, 
{
    "Document_Type": "CC", 
    "Document": "53080422"}
{
    "Document_Type": "NIT", 
    "Document": "53080422"}
]
  1. i think in this but is incorrect because i can have registers with the same Document but different Document_Type.
db.person.find
({
"Cliente.Document": { $in: clients.map(s => s.Document) },
"Cliente.Document_Type": { $in: clients.map(s => s.Document_Type) }
})
  1. This solution dont show me anything.
clients.forEach( function(register) {
db.person.find({
"Cliente.Document": register.Document,
"Cliente.Document_Type": register.Document_Type
})
} );

If someone can help me I would really appreciate it, and sorry for my English

CodePudding user response:

Is it possible to use the $or operator?

db.person.find({
  $or : [
           {"Document_Type": "CC", "Document": "52879473"},
           {"Document_Type": "CC", "Document": "19294395"},
           {"Document_Type": "CC", "Document": "53080422"},
           {"Document_Type": "NIT", "Document": "53080422"}
        ]
});

You will see that is the same as

db.person.find({ $or : clients });

I haven't tested it myself.

  •  Tags:  
  • Related