$ne query not working with mongoose but working in mongoshell

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

Answers


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 );

Need Your Help

Get mutiple multiple rows value base on array id IN CAKE PHP

php cakephp cakephp-2.0 cakephp-1.3

i want to get multiple rows value base on multiple id value and i m getting id in array.

UniqueConstraint in EmbeddedConfiguration

c# db4o

I just started using db4o on C#, and I'm having trouble setting the UniqueConstraint on the DB..