Get value from a dictionary using MongoDB

Hello Team, I have below kind of data:

{ _id: 1,
"profile": { "dept" : "Other Healthcare", "iid" : "ams_sales", "title" : "Other" } “Item_qnty”: 0
} {
_id: 2, “profile”: { “dept” : “Other Healthcare”, “iid” : “ams_sales”, “title” : “Other” }
Item_qnty: 10}
{ _id: 3,
"profile": { "dept" : "Healthcare", "iid" : "sales", "title" : "Healthcare" } Item_qnty: 6

I have to extract only Item_qnty for each document in a loop.
I tried below statement but as it is findone so it returns only one value:

db.test.findOne().Item_qnty 6

I have huge volume of records/documents. I need only value from each document in a loop.
`0, 10, 6

1 Like

Thank you… but unfortunately db.collection.find() has no solution. I already mentioned in my queries. though I create a function and got the result, not exactly with the value but by some other way.

You will need to understand the concept of projection.

db.test.findOne().project({Item_qnty: 1, _id: 0});

_id is default to ON so we need to set it OFF, all other fields default OFF so we set Item_qnty to ON.

1 Like

Many thanks for your time, contribution and support, but it doesn’t meet the requirement. I am getting below result:
Item_qnty: 0
Item_qnty: 10

I dont want KEY “Item_qnty” I only want value 0,10,6 nothing else

  { $group: { _id: null, items: { $push: "$Item_qnty" } } }
1 Like

Sir, as I mentioned in earlier reply to someone else that I wrote a function and achieve whatever I want. I didnot get exactly the value of a Key but the end goal has been achieved. I need value to keep in loop and deduct the same from another value. Below code is working fine for me and my requirement: const currDate = new Date();
// Array of collection names
const collectionNames = db.getCollectionNames();
// Iterate over each collection
for (const collectionName of collectionNames)
const collection = db.getCollection(collectionName);
// Get documents with retention_days field
const documents = collection.find({ retention_days: { $exists: true } });
// Iterate over each document
documents.forEach (document =>
const retentionDays = document.retention_days;
const watermark = new Date(currDate.getTime() - (retentionDays));
all_insts = [collectionName] + list(collectionName.descendants)

for (inst in all_insts) 
{'enforcing retention for institution %s',
    users = User.find_by_iid(;
   /* // gather up user id's and notify once
    notify_user_ids = set();
    for (user in users) {'enforcing retention for user %s', user._id);

// Perform further operations with the watermark
// ...


your code also gave me nearly requested output as below:
_id: null,
retention_days: [