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...