$ne query not working with mongoose but working in mongoshell

mongodb, mongoose, node.js

Solution

Because `financedProjects` is an `array` you have to address the element with `[]` like:

FinancedProject.find({
    _id: {
        $ne: fb.financedProjects[0|.financedProjectId
    }
}).exec( callback );

EDIT:

mongoose ist JavaScript, so it follows the rules of JavaScript. `fb.financedProjects` is an `array`. So if you use the expression `fb.financedProjects.financedProjectId` this is evaluated to `undefined` by the JavaScript interpreter, because there is no `financedProjectId` property within that array (arrays have `0`,`1`,`2`,`3`,... as properties). So mongoose does get `{ $ne: undefined }` and has no chance to recognize that you meant the property `financedProjectId` of the array elements.

To achieve what you want, you can do this:

var arr = [];
for( var i=0; i<fb.financedProjects.length; i+=1 ) {
    arr.push( fb.financedProjects[i|.financedProjectId );
}
FinancedProject.find({
    $not: {
        _id: {
            $in: arr
        }
    }
}).exec( callback );

Problem

When I execute this mongoose query ``` FinancedProject.find({_id:{$ne:fb.financedProjects.financedProjectId}).exec( callback); ``` where fb is an object like this ``` { _id: ObjectId("54das4da9dsa9d4ad4a9"); name: "some", financedProjects: [ {registry:"147", financedProjectId:ObjectId("13da4sd4sa48da4dsa")}, {registry:"189", financedProjectId:ObjectId("5d5asd5a4sd5ada5sd")} ] { ``` the result is undefined and when I execute it in the mongoshell the results are the expected

Original source