How to access the array of array in mongoDb - mongodb

I'm facing with mongodb for querying the array of array fields
I am able to access the 'feild1.feild2' but its returns the value
field1:{field2:[Array]}
field1:Object
field2:[Array]
0:Array
0:Object
My Real Dataset are:
customerData:Object
sendtocustomerlist:[Array]
0:Array
0:Object
Name:xxx
phone:xxxxxxx
I want to access the field2 and the nested arrays to get the Object values. Help with that.

you can use arrayElemAt:
here is the link:
https://docs.mongodb.com/manual/reference/operator/aggregation/arrayElemAt/#exp._S_arrayElemAt

Related

Does MongoDB have set for data structure?

I have a list of user ids stored in a field of MongoDB collection. Each of the user ids is an UUID. The field is an array. For example,
["59cc7562-751b-11ed-a1eb-0242ac120002", "66e6f240-751b-11ed-a1eb-0242ac120002",
"742df8ea-751b-11ed-a1eb-0242ac120002", ............,"6cf6e398-751b-11ed-a1eb-0242ac120002"]
Now, if I want to check whether a user id is in the array, i need to do loop through the array until I find the user id or reach the end of the array. This is slow. I wonder if I can use a set to store the user ids instead of an array.
I am using Next.Js in the backend. If I convert the array to a set, that would be even slower than just looping through the array since the conversion takes o(n).

Query a Map containing a string saved in Cloud Firestore with Dart/Flutter

I've a problem with a query condition. I would like to query a string inside an array that's also inside a map. I manage to receive the information complet without the where condition but when I do this condition I've an error.
My function to retrieve all information
My array with a map inside
If category is the only value inside the produits array items, you can check for its existence with array-contains:
collectionRef.where('produits', arrayContains({ 'categorie': 'Alimentation' }))
If there are more properties for the array item, you will need to specify all of them in the call to arrayContains. There is no way to query for a subset of the array item.
In such cases, a common workaround is to add a extra array field to the document with just the property values you want to query for. For example:
produitsCategories: ['Alimentation', '...']
With that field in place you can then query it with:
collectionRef.where('produitsCategories', arrayContains('Alimentation'))

How can you create a Firestore compound query that returns all documents in an array of mapped objects?

I want to avoid returning documents from Firestore that I do not need. I have a collection of documents with the following properties:
membership {array}
0 {map}
contactGroupId: "1da9a73f89f8ca4c"
contactGroupResourceName: "contactGroup/1da9a73f89f8ca4c"
1 {map}
contactGroupId: "1da9a73f89f8ca4c"
contactGroupResourceName: "contactGroup/1da9a73f89f8ca4c"
I want to create a compound query that only returns documents if the contactGroupId is equal to 1da9a73f89f8ca4c. Something like ref.where(membership.{key}.contactGroupId, "==", "1da9a73f89f8ca4c"). Is this possible?
It's not possible to filter documents using the contents of a map field inside an array. You will need to restructure your data if you want to perform this query. You have two options.
Put all of the contactGroupId values into their own array field, and use an array-contains query to find them.
Instead of using an array, use the contactGroupId value as the name of a map key that you can use in a query, like where(membership.1da9a73f89f8ca4c.present, '==', true). Your membership field would be structure like this:
membership (map)
- 1da9a73f89f8ca4c
- present (boolean)
- contactGroupResourceName = "contactGroup/1da9a73f89f8ca4c"

mongodb - querying & projecting element value from nested array

I have a nested arrays as below in one of my collections,
how to retrieve the value from specific sub array & i don't need any of the other fields in that document.
I have tried using following query but it didn't get me the expected output,
query:
db.getCollection('my_collection').find({"_id":"08d4608a-e6c4-1bd8-80e6-8d1ac448c34b"},{"_id":0,"customProperties.0.1":1})
You can try
db.getCollection.find({
"someArray.someNestedArray.name": "value"
})

mongodb: count/size of a query array

How do I get the size .count of a returned array
db.students.distinct('class_id')
This query will get back a array, how do I get the size/length/count of this array?
Remember this is javascript code, thus you can get the count of an array using length.
db.students.distinct('class_id').length
db.students.distinct('class_id').length