jeudi 19 février 2015

Mongoose $elemMatch find via "_id" NOT working, alternative field working?

Matching both a username and the label of an item, DOES return the following document (among any other matching docs), but querying with the _id instead of the label does NOT?


There may be many users in the users array, and there may also be many tags in the tags array of each user..


Here's a sample Items Doc:



{
"users": [
{
"username": "user1",
"tags": [
{
"__v": 0,
"label": "label1",
"_id": "54dd29c2855535000037cf85"
}
]
}
]
}


And here's the ATTEMPTED nested $elemMatch find(), which currently returns an EMPTY array:



Models.Items.find({
"users": {
"$elemMatch": {
"username": "user1",
"tags": {
"$elemMatch": {
"_id": "54dd29c2855535000037cf85" // does NOT work
}
}
}
}
})


Where as this nested $elemMatch find() DOES return the matching documents:



Models.Items.find({
"users": {
"$elemMatch": {
"username": "user1",
"tags": {
"$elemMatch": {
"label": "label1" // DOES work
}
}
}
}
})


Here are the schemas that are involved (stripped to the relevant fields):



var tagSchema = new Schema({
label : String
});

var userItemSchema = new Schema({
username : String,
tags : { type : Array , "default" : [] }, // is this the issue?
});

var itemsSchema = new Schema({
users : [ userItemSchema ]
});


The 'empty by default' tags was a recent solution for building new arrays of 'tagSchema' document objects to immediately push in batch to the 'tags' field of some 'userItemSchema'.. Here was the intention:



var userItemSchema = new Schema({
username : String,
tags : [{
label : String,
tag : { "type": Schema.ObjectId, "ref": "tag" }
}],
});


..but building these tag objects in a node.js promise, to push to a particular user does not seem to work as a structured push to an array of objects..Promise.map not finshing because subsequent Promise.join finishes first? Promise.all?


Aucun commentaire:

Enregistrer un commentaire