how can I get field and value from nested array and query them to find doc in mongodb?

Mohit Kumar

I am trying to get field and values from a nested array which is dynamic(List1 and List2 ar dynamic )

let selectionCritea = {
  List1: [ 'mk singh', 'saurav kashsyap' ],
  List2: [
    { labelName: 'Location', values: ['India','America','Japan'] },
    { labelName: 'gender', values: ['male'] }
  ]
}

I want to find doc from db which has properties as "labelname" and values from corresponding "values" array i.e. gender is male and Location is either India ,America or Japan. here is my modelschema:

{ "_id" : "6123e7ahdhdfdj334733",
    "name" : "Mohit Kumar",
    "gender" : "male",
    "Location" : "India",
    },
   { "_id" : "6123e7ahdhdfdj334731",
    "name" : "saurav  Kashyap",
    "gender" : "male",
    "Location" : "Nepal",
    },
   { "_id" : "6123e7ahdhdfdj334720",
    "name" : "Shaline",
    "gender" : "female",
    "Location" : "america",
    },

so I should get only first doc because gender is male and location in from values array. I have tried all my knowledge but couldnot get it

turivishal

First of all you need to prepare a query in client side,

let selectionCritea = {
  List1: [ 'mk singh', 'saurav kashsyap' ],
  List2: [
    { labelName: 'Location', values: ['India','America','Japan'] },
    { labelName: 'gender', values: ['male'] }
  ]
}
let query = {};

// get regular expression query condition
let names = [];
selectionCritea.List1.forEach(q => {
  names.push(q)
})
if(names.length) {
  query.name = { $regex: names.join("|") }
}

// prepare List2 conditions
selectionCritea.List2.forEach(q => {
  query[q.labelName] = (Array.isArray(q.values) ? { $in: q.values } : q.values);
})

console.log(query)

You can use the above prepared query in find query:

db.collection.find(query);

Playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get specific field value with array nested filter in MongoDB?

How can I loop through nested json and get array unique value from field

how can I find the lowest value of each row and exclude them to find a new average from array

How can we update the value of a array field/add a field in nested array in mongodb with a custom value

How can I query for userId's in a nested Array schema with MongoDB?

How to get the value of a field from mongoose mongodb query?

How can i aggregate filter nested documents and get value from other field

How can I get the total sum ($sum) of an array from a nested field?

How can I query a jsonb array to find if it contains a value from a query list?

How can I unset a value within a nested array of objects? MongoDB

How can I insert value in array inside object field in mongodb?

How can I get the fields from another mongodb collection and add those field values as an array in the current collection?

How can I find a specific value inside a nested array?

How can i get non String Array field from SQL query with multiple rows

How can I get a value from an array?

In mongoDb, how can I rename a field nested like object-> object -> array -> object -> field?

how i can get data from nested array and get the result

how can i get an array after match query in mongodb aggregation

How can I get the value of nested JSON array in Ionic?

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform). How can I get a doc from firestore?

firebase / how i can to get a field from all doc's in firestore / javascript

How to get value of field in array mongodb

How can I get a nested value in a JSONObject from its path?

How can I get value from object with nested object?

How can I get a value from a JSON with a partially field name

How can I get a field value from a document?

How to get an object from a MongoDB nested array

In MongoDB query, how to get a field in _id included nested document

mongodb, how can I project or push root field on nested array using aggregate?