I need to be able to all doc from a collection with the user id's macthes any of the comment id's in the sub array of docs.
This is the schema:
var postSchema = mongoose.Schema({
user_id : String,
users_full_name : String,
username : String,
start_time : String,
post : String,
comments : [{
user_id : String,
start_time : Date,
comment : String,
cancelled : Boolean,
cancelled_time : Date
}]
});
When there was just one doc in the array this worked fine:
modelPost.find({
"comment.user_id": user_id,
"comment.cancelled": {$ne:true}
}).sort({"post.start_time": -1}).find(function( err, posts ){
if( err ){
console.log( err );
}
return callback( posts );
});
But as soon as i put another object in the comments array the query doesn't return anything.
It does work however when i remove the "comment.cancelled": {$ne:true} bit. But I need this bit...
The comments object could have 3 objects in it, however two could belong to one user with one of the two cancelled eg:
user_id : 123,
users_full_name : 'Bob Smith',
username : 'bob.smith',
start_time : some date,
post : 'some very interesting post on something cool',
comments : [{
user_id : 654,
start_time : some date,
comment : 'a nice comment',
cancelled : true,
cancelled_time : some time,
},{
user_id : 654,
start_time : some date,
comment : 'a nice comment'
},{
user_id : 701,
start_time : some date,
comment : 'a nice comment',
cancelled : true,
cancelled_time : some time,
},{
user_id : 888,
start_time : some date,
comment : 'another nice comment'
}]
When the document is filled like this the above query yields no results. When i take the "comment.cancelled": {$ne:true} it simple returns all comments belonging to the given user id, included the ones with cancelled = true.
It is as if the "comment.cancelled": {$ne:true} is being cross-ref against the entire array, it finds on object in the array with comment.cancelled=true thus does not return the entire post doc.
With the above example, i if the user id in the query was 654 the query would return the full post. However if the id in the query was 701 it would return no results. And obv. 888 would return the post.
Currently I have to do post query op to filter the results. in the '701' scenario.