mongodb: count/size of a query array - mongodb

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

Related

How to access the array of array in 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

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"
})

How do we limit the index for retrieved array document in mongoDb

lets take a sample collection with dummy data
{
_id:*******
value:[{id:'*',value:'12'},{id:'*',value:'12'},{id:'*',value:'12'},{id:'*',value:'12'}...]
}
I found that data can be pushed in array by $push but how will i be able to get the array in mongoDb with limited index, i.e if i want the value from only zero index to 20 not all at once.
Try this:
db.coll.find({}, {'value':{$slice: 20}});

What's the easiest way to return the results of a query for a given key/value pair in mongo as an array of the values returned?

I have a field called id (not _id) in documents from two collections. I need to compare the contents of the first collection with the second. Basically, I need to know what documents with a given value 'id' exist in collection 'A', but not 'B'. What's the easiest way to build an array of id's from Collection A that I can use to do something like the following. :
db.B.find({id:{$nin: array_of_ids_from_coll_A}})
Please don't get hung up over why I'm using 'id' in this case, and not '_id'. Thanks.
Strictly speaking, this doesn't answer the question of 'how to build an array that...', but I'd iterate over collection A and, for each element, try to find a match in B. If none is found, add to a list.
This has a lot of roundtrips to the database, so it's not very fast, but it's very simple. Also, if A contains a lot of elements, the array of ids might be too large to throw all of them in the $nin, which otherwise would have to be solved by splitting up the array of ids. To make matters worse, $nin isn't efficient with indexes anyway.
I incorrectly assumed that the function 'distinct' returned a set of distinct documents based on a given 'field'. In fact, it returns an array of distinct values, provided a specific field. So, I was able to construct the array I was looking for with db.A.distinct('id'). Thanks to anyone who took the time to read this question, anyway.

how to add a dictionary to array in Mongodb

I Use from Mongodb and have a collection like this:
{'name':'vahid','visited':[{id:1,'date':'1223123',noskhe:['a','d','h']]}
I want an update query to add {id:2,'date':'324324',noskhe:['d','n']} to visited array!
How I can get this query?
You need to use $push operator. It will append a new value to the array. Below is the example.
db.collection.update({"name":"vahid"},{$push:{ "visited": {id:2,'date':'324324',noskhe:['d','n']}}})