0

I have multiple documents stored in a single collection. I want to be able to go through each document and get the ObjectID value from each one and store them into a single array. I am not sure how to go about doing this. This is what I have so far:

app.get("/sessionID", (req, res) => {
use iRateIt;
var value = iRateIt.responses.count();
for (var i = 0; i < value; i++){
  //code to store ID values in an array
  }
});
  • Not really that clear what you are asking, or at least from the perspective of "why" you are even asking it. There is no practical reason to retrieve every document in a collection. Where possible to do so, then you probably should not be using a database anyway. It's likely your real purpose is to "do something" with the data before you return it. What your question is lacking is describing exactly what that "something" is. – Neil Lunn May 10 '18 at 03:54
  • Possible Duplicate of : [How to select a single field in MongoDB?](https://stackoverflow.com/questions/25589113/how-to-select-a-single-field-in-mongodb) – Neil Lunn May 10 '18 at 03:56

1 Answers1

0

You could try the following aggregation query . However,if there are large number of documents it is probably of no use.

db.<collection>.aggregate([{"$group":{"_id":null,"idArray":{"$addToSet":"$_id"}}}])
mintekhab
  • 203
  • 1
  • 3