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

317
Views
Comodín para la clave en la consulta mongodb

Tengo una colección equivalente a:

 [ { "_id": ObjectId("5a934e000102030405000000"), "sides": { "0": { "dist": 100 }, "1": { "dist": 10 } } }, { "_id": ObjectId("5a934e000102030405000001"), "sides": { "0": { "dist": 100 } } } ]

Me gustaría realizar una consulta que devuelva cualquier documento que tenga una clave anidada en sides que tenga la clave dist con un valor específico. Algo como:

 db.collection.find({"sides.*.dist": 10})

Aquí * actúa como comodín, cualquier clave sería válida en su lugar.

Eso recuperaría:

 [ { "_id": ObjectId("5a934e000102030405000000"), "sides": { "0": { "dist": 100 }, "1": { "dist": 10 } } } ]

Por otro lado

 db.collection.find({"sides.*.dist": 100})

Recuperaría ambos documentos.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

la siguiente canción y baile no serán necesarios si el campo de los sides fuera una matriz ...

 db.collection.find( { $expr: { $gt: [{ $size: { $filter: { input: { $objectToArray: "$sides" }, as: "x", cond: { $eq: ["$$xvdist", 10] } } } }, 0] } })
over 4 years ago · Santiago Trujillo Report

0

Puede obtener los elementos coincidentes usando este

 db.collection.aggregate([ { "$project": { "sides_array": {//Reshape the sides "$objectToArray": "$sides" } } }, {//Denormalize to get more than one matches "$unwind": "$sides_array" }, { "$match": {//Condition goes here "sides_array.v.dist": 10 } }, { "$group": {//Group the data back, after unwinding "_id": "$_id", "sides": { "$push": "$sides_array" } } }, { "$project": {//Reshape the data "_id": 1, "sides": { "$arrayToObject": "$sides" } } } ])
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!