Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Filtro MondoDB basado en una matriz de objetos

Tengo este tipo de datos en una colección:

 { "_id": { "$oid": "6014662b3a38ba4929b602ab" }, "collection": [ { "name": "Facility", "_id": { "$oid": "601466293a38ba4929b602aa" }, "is_deleted": false }, { "name": "User", "_id": { "$oid": "60021529c02fa2b27e7c1989" }, "is_deleted": false }, { "name": "Asset", "_id": { "$oid": "6018948fc35a109224439f73" }, }, { "name": "Asset", "_id": { "$oid": "604a734fca10c74d800031db" }, "is_deleted": true } ], "customer": "TestCustomer", "is_deleted": false }

y tambien tengo esta consulta

 db.getCollection("Collection").aggregate([{ '$match': { '$and': [{ 'collection.name': 'User', 'collection._id': ObjectId('60021529c02fa2b27e7c1989'), 'collection.is_deleted': false }, { 'collection.name': 'Asset', 'collection._id': ObjectId('604a734fca10c74d800031db'), 'collection.is_deleted': false }], 'customer': 'TestCustomer', 'is_deleted': false } }])

is_deleted on Asset es verdadero en la base de datos, pero estoy solicitando cuando el valor es false .

Cuando se ejecuta esta consulta, no se supone que devuelva ningún dato, pero lo hace. He estado tratando de resolver este problema, pero nada ha ayudado hasta ahora.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debido a que está tratando de hacer coincidir elementos dentro de una matriz, debe usar $elemMatch en su lugar. Además, no hay necesidad del operador $and .

 db.collection.aggregate([ { "$match": { "collection": { "$elemMatch": { "name": "User", "_id": ObjectId("60021529c02fa2b27e7c1989"), "is_deleted": false } }, "collection": { "$elemMatch": { "name": "Asset", "_id": ObjectId("604a734fca10c74d800031db"), "is_deleted": false } }, "customer": "TestCustomer", "is_deleted": false } } ])

Zona de juegos: https://mongoplayground.net/p/cdz9TgQ8HNw

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!