Use $or with nested array of objects in mongodb

Kalanit

I have the following collection

[
  {"overlaps": [{"BB1": "itemA", "iou": 0.1,  "BB2": "itemB"},{"BB1": "itemB", "iou": 0.4,  "BB2": "itemC"}], "City": "Paris", "mode": "RGB","path": "photo1.png"},
  {"overlaps": [{"BB1": "itemA", "iou": 0.5,  "BB2": "itemC"}], "City": "London", "mode": "RGB","path": "photo2.png"},
  {"overlaps": [{"BB1": "itemB", "iou": 0.8,  "BB2": "itemB"}], "City": "London", "mode": "greyscale","path": "photo3.png"},
  {"overlaps": [{"BB1": "itemA", "iou": 0.2,  "BB2": "itemC"},{"BB1": "itemA", "iou": 0.8,  "BB2": "itemC"}], "City": "Berlin", "mode": "RGB","path": "photo4.png"},
  {"overlaps": [{"BB1": "itemA", "iou": 0.9,  "BB2": "itemB"}], "City": "NY", "mode": "greyscale","path": "photo5.png"},
  {"overlaps": [{"BB1": "itemA", "iou": 0.8,  "BB2": "itemB"}], "City": "Roma", "mode": "RGB","path": "photo6.png"}
]

I would like to retrieve documents that

  • have city = berlin

  • OR have mode = greyscale

  • OR countains at least one overlap between "BB1":"itemA" and "BB2":"itemC"

The first 2 conditions are easy:

cursor = record1.find({"$or": [{"City":"Berlin"},{"mode":"greyscale"}]}) 

How can I add the third condition to the query?

Ashh

You should need to apply $elemMatch with $or condition to obtain the result

db.collection.find({
  $or: [{ City: "Berlin" }, { mode: "greyscale" },
    {
      overlaps: {
        $elemMatch: {
          $or: [
            {
              BB1: "itemA",
              BB2: "itemC"
            }
          ]
        }
      }
    }
  ]
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use find for nested object of objects in MongoDB

Mongoose MongoDB: updating objects in a nested array

how to query nested array of objects in mongodb?

MongoDB - calculating average of nested array of objects' attributes

removing object from nested array of objects mongodb

Find objects in nested array, mongodb, compass

unwind and group for the nested array objects in mongodb

Update nested array objects based on a property in MongoDB

MongoDB Get Single Object Nested in Array of Objects in Array of Objects

Use .find nested in .map to build array of objects

query mongodb inside nested array of objects

mongoDB aggregate lookup on nested array of objects

How to use $filter in nested child array with mongodb?

How can I use latest MongoDB C# Driver to delete an element within a nested array of complex array objects based on multiple conditions?

MongoDB aggregation with nested array of objects property with date

MongoDB: aggregate lookup in deeply nested array of objects

MongoDB aggregation use $elemMatch for nested array

Comparing objects inside a nested array - mongoDB

Filtering and measuring mongodb nested array of objects

Querying array of nested objects in MongoDB

See if nested array of objects contains value | MongoDB

How to use _sortBy in an array of objects that contains nested array of objects

mongodb: updatemany against nested array objects

MongoDB: How to filter nested array of objects

MongoDB query for documents with nested objects and array

How to set values of nested array of objects in mongodb

MongoDB - Update nested array with many nested objects

filtering MongoDB array of Nested objects

MongoDB aggregation from nested array of objects

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive