Distinct dict keys from array of dict - mongodb

I am working on a project using MongoDB. I have so far managed to upload documents to the database. Records looks like
doc1 = {'customer_id': 1,
'order': "[{'item': 123, 'unit_price': 2.0}, \
{'item': 124, 'unit_price': 2.5}]"}
doc2 = {'customer_id': 2,
'order': "[{'item': 123, 'unit_price': 2.0}, \
{'item': 126, 'unit_price': 1.5}]"}
collection.insert(doc1)
collection.insert(doc2)
I would like to find distinct items from the collection. My attempt so far
db.collection.distinct("order.item")
Expected output:
[123,124,126]
This does not quite work since order is an array. I am looking for some way to unnest the array to get distinct item numbers. Eventually, I would like to get the count of those distinct items. Thanks for your help!

MongoDB does return distinct for nested items from an array of objects:
https://docs.mongodb.com/manual/reference/method/db.collection.distinct/#return-distinct-values-for-an-embedded-field
In your case db.collection.distinct("order.item") should return array of distinct items from orders collection

Related

Mongo template not returning all records

criteria = Criteria.where("gstin").in(gstinList);
in db gstin number 1234, 123, 1234.
when i want to retrieve the gstlin list by giving input 1234, 123, i am getting only 2 records not 3 records.
what is the issue here not able to get it

Match up instances 2 lists in pymongo

I am using pipelines in pymongo to query a json file.
I have one list, "sixcities" containing the 6 'cities' with the 'highest count' of book shops i.e. the least book shops. (contains 6 pymongo instances)
{'_id': 'city1', 'count': 84}
{'_id': 'city2', 'count': 65}
{'_id': 'city3', 'count': 61}
{'_id': 'city4', 'count': 59}
{'_id': 'city5', 'count': 84}
{'_id': 'city6', 'count': 64}
I have a second list, "travelcities" with the counts of Travel Book shops in each of the 'cities' ( 20+) in the json file. (contains 20+pymongo instances)
{'_id': 'city1', 'count': 42}...etc
Please note:This list holds cities that do not feature in the first list.
I would like to use these lists to calculate the ratios of travel book shops in the 6 highest count cities.
The common key will be 'city' as this appears in documents of both lists
i.e. in list 2 : city1: 42 divided by in list 1: city1: 84 = 0.5 ratio
I am unsure of how to do this in pymongo as the information is in mongo documents within a list.
I thought some kind of nested loop would work:
dict={}
for i in sixcities: #loop through the first list
dict[i["_id"]]=i["count"]
for i in travelcities: #loop through second list
dict[i["_id"]]=i["count"]/(dict[i["_id"]]) #ratio
But I am getting the following result:
KeyError: 'city15'
This city does not appear in the first list as one of the 6 with the most bookshops, but it does appear in the second as containing a travel bookshop.
Any and all help is appreciated.
One of the problems in your code is that you are using same variable 'i' in both outer and inner loop
Consider this code which, for each city in first list search for it in the second list, then computes the ratio.
dict={}
for i in sixcities: #loop through the first list
dict[i["_id"]]=i["count"]
for j in travelcities: #loop through second list
if j["_id"] == i["_id"]:
dict[i["_id"]]=j["count"]/(dict[i["_id"]]) #ratio
Do note that if the city does not exist in the second list the answer remains the count of the city in the first list. Handle this corner case in the way you want.

Sorting with a ReactiveMongo JSON query

This seems crazy that I have to ask but I cannot find the right syntax to sort query result using ReactiveMongo. So if I had this:
rCollection.flatMap(
// find all
_.find(Json.obj())
// perform the query and get a cursor of JsObject
.cursor[Resort](ReadPreference.primary)
// Collect the results as a list
.collect[List](Int.MaxValue, Cursor.FailOnError[List[Resort]]())
)
How would I sort by a particularly column in descending order.
This would probably be:
rCollection.flatMap(
// find all
_.find(Json.obj())
.sort(Json.obj())
// perform the query and get a cursor of JsObject
.cursor[Resort](ReadPreference.primary)
// Collect the results as a list
.collect[List](Int.MaxValue, Cursor.FailOnError[List[Resort]]())
)
The JSON object you send to the 'sort' function would be the same JSON you'd use in mongo query for sorting. eg:
{"age": 1, "lastName": -1}
sorts by age ascending and then lastName descending.

MonogDB: How to get all the documents with specific value in a field?

I have an array of 10 unique Object IDs named Arr
I have 10,000 documents in a collection named xyz.
How can I find documents using Object IDs in the array Arr from the collection xyz with only one request?
There are $all and $in operators but are used to query fields with an array.
Or do I need to make requests equal to the length of Arr and get individual document using findOne?
EDIT:
I'm expecting something like this:
db.getCollection("xyz").find({"_id" : [array containing 10 unique IDs]})
....for which the result callback will contain an array of all the matched IDs of query array.
According to the documentation here: https://docs.mongodb.com/manual/reference/operator/query/in/
You should use the following query:
db.getCollection("xyz").find({"Arr" : { $in: [123, 456, 789 ] }});

query find where element in array

my documents "parents" got the folowing structure:
{childrenIdList: [23, 24, 34]}
{childrenIdList: [23, 88]}
{childrenIdList: [1, 5, 8]}
how to select parents by childId in there childrenIdList?
Such query must return first two documents of 3 in my example if childId = 23.
I tried to use elemMatch method, but seemingly it works only with objects, i.e. it would work only if my data would be {childrenIdList: [{Id: 1}, {Id: 5}, {Id: 8}]}
You can just use db.collection.find({childrenIdList: 23}). See the Query Arrays section in the manual for more details.
db.collection.find({"parent.childrenIdList": {$in: [23]}})