1

I store documents in different versions in MongoDB. I upload a new version of the same document where it can change status. I will identify them with a GUID.

My model:

var Issue = new Schema({
  DatePosted: {type: Date, default: Date.now}
  GUID: {type: String},
  Status: {type: String},
}

and lets say I have some documents:

{[
  DatePosted: "2015-09-21T15:46:39.753Z",
  GUID: "199b4534-8ffb-4045-a9d3-b71fc61298f4",
  Status: "Status 3"
],
[
  DatePosted: "2015-09-20T15:46:39.753Z",
  GUID: "199b4534-8ffb-4045-a9d3-b71fc61298f4",
  Status: "Status 1"
],
[
  DatePosted: "2015-09-21T15:46:39.753Z",
  GUID: "3c53a75e-e31e-49e4-8be0-0b94e591758f",
  Status: "Status 2"
],
[
  DatePosted: "2015-09-20T15:46:39.753Z",
  GUID: "3c53a75e-e31e-49e4-8be0-0b94e591758f",
  Status: "Status 0"
]}

I now wish to only get the latest version of the documents based on the GUID. So in this case I would expect this as result:

{[
  DatePosted: "2015-09-21T15:46:39.753Z",
  GUID: "199b4534-8ffb-4045-a9d3-b71fc61298f4",
  Status: "Status 3"
],
[
  DatePosted: "2015-09-21T15:46:39.753Z",
  GUID: "3c53a75e-e31e-49e4-8be0-0b94e591758f",
  Status: "Status 2"
]}

My approach has been to first get all the unique GUIDs and then try to get the latest version of each document by the unique GUIDs.

var guidArr = [];

Issue.find()
  .where('GUID')
  .distinct('GUID')
  .exec(function(err, docs) {
    guidArr = docs;
  });
// -> guidArr = ['199b4534-8ffb-4045-a9d3-b71fc61298f4', '3c53a75e-e31e-49e4-8be0-0b94e591758f']

Issue.find({
  'GUID': {$in: guidArr}}, function(err, docs) {
  res.status(200).send(JSON.stringify(docs));
});
// -> docs = []

This will return an empty object []... But if I write the GUIDs manually like

Issue.find({
  'GUID': {$in:
    ['199b4534-8ffb-4045-a9d3-b71fc61298f4',
     '3c53a75e-e31e-49e4-8be0-0b94e591758f']}}, function(err, docs) {
  res.status(200).send(JSON.stringify(docs));
});
// -> docs = all documents in the db that has those GUIDs

this will return all object in the collection (4 documents in this case).

Two questions: Why do I get an empty object in return when I give find() an array (guidArr), but when I give it hard coded array everything seems to be working?

How do I get the latest version of the documents in the second find()?

Can I do this in one chained question to the database? My approach doesn't seem like best practice...

mottosson
  • 3,283
  • 4
  • 35
  • 73
  • 1
    I think your best best here would be to use the [**aggregation framework**](http://docs.mongodb.org/manual/core/aggregation-introduction/) where you group the documents by `GUID` field and extract the latest version with the [**`$max`**](http://docs.mongodb.org/manual/reference/operator/aggregation/max/) accumulator operator. – chridam Sep 25 '15 at 07:43
  • 2
    Your second query is empty because `guidArr` is still empty at that point because `find` is async. See [this post](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). As for how to do this better, @chridam's suggestion is right on. – JohnnyHK Sep 25 '15 at 12:26
  • Oh asynchronicity, you got me again! Thanks, that solved my first question. – mottosson Sep 25 '15 at 12:42

0 Answers0