Using Aggregate in MongoDB to read a single value out of an indexed array output

Hi everyone!

I am extremely new to MongoDB including using this community forum and using its various functions of coding. I have scoured through multiple pages of documentation online within Mongo DB and google itself and I seem to still be stuck on the solution to my problem. Consider I have the following documents in my Test collection

{"_id":{"$oid":"1"},"channelID":"1","content":"yup","messageID":"1","stick":"off","__v":{"$numberInt":"0"}}```
{"_id":{"$oid":"2"},"channelID":"2","content":"No","messageID":"2","stick":"off","__v":{"$numberInt":"0"}}``
{"_id":{"$oid":"3"},"channelID":"3","content":"hi","messageID":"3","stick":"off","__v":{"$numberInt":"0"}}

I need each outputted document to stay together as a whole, but also I need to be able to pull each field key within that document. For example my current method of pulling a single document is by using an aggregate function:

const testing2 = await test.aggregate(
   [ 
    { $project : { _id: 0, content : 1 , channelID : 1, stick:1 } 
  } 
] 
)

This will give me all of my documents in the collection like needed. I then index the documents to pull the specific one I need. For example console.log(testing2[0]) will pull the first document in the collection, so it would show:

{"channelID":"1","content":"yup","stick":"off",}

From then, I still need to get the three specific values within that indexed document - content, messageID, and channel ID, but i am unable to figure out how to further breakdown this newly outputted document to only show the values I need. For instance, if I wanted to pull only the “content:” portion out of the testing2[0] document I need the desired output to show:

“yup”

If I needed to pull the “channelID:” value from the testing2[0] document I need the desired output to show:

“1”

I have already tried to use the distinct() method of pulling each individual field within the collection, which works fine for pulling those specific fields, but doesn’t always match up together in the end with the way Mongo sorts each of the documents. For example I have tried:

var content = ((await test.distinct("content")));
`
`
var channel = (await test.distinct("channelID"));
`
`
var stick = (await test.distinct("stick"));

Which will pulls those specific fields out of the collection. So with trying to index the first document, I naturally tried:
console.log (content[0] + channel[0] + stick[0])
for the desired output of:
"yup", "1", "off"
However it seems to pull differently with how MongoDB ranks each document within the collection for each key, so instead of it pulling the first document in the collection, the desired out put varies each time, so I might receive an output of:

“yup”, “2”, “on”
Or
“hi”, “1”, “off”

depending on how Mongo decides to pull. I am using the most up to date version of MongoDB as well as the most up to date version of node.js and running the code through the software VSCode. If anyone has a solution to my it would be so much help - I am unsure if I am missing something somewhere because I am so new to this or if there is just something I’m not doing correctly.

The distinct operator will get all unique values for that field within the collection, so something like:

db.getCollection('Books').distinct('Authors')

Will return the distinct list of authors over all documents within the Books collection.

For your case, unless I’m just misunderstanding your question you just want to process data as pulled back, you should note that if order is important you’re not supplying a sort criteria, within the aggregate this can be done via $sort stage.

Other than that the node driver will be returning a cursor to the docs, if you want to loop over it, take a look at the docs:

Here is a linked sample project, looping over results of an aggregation query in node:

1 Like

Thank you so much for the reply! I was able to get it sorted out using the links you provided. It seems like I was overlooking and misunderstanding some information before!

Excellent, glad you got it sorted!