samedi 5 juin 2021

Mongoose $and Filter doesn't Filter Properly

The $and operator in Mongoose isn't working properly in NodeJS. Let's assume we have this JSON-MongoDB data:

{ 
    "_id" : ObjectId("60bb4cd74a802722d8b0de0f"), 
    "username" : "System", 
    "text" : "matan has joined!", 
    "__v" : NumberInt(0)
}
{ 
    "_id" : ObjectId("60bb4cd74a802722d8b0de10"), 
    "username" : "System", 
    "text" : "Welcome!", 
    "__v" : NumberInt(0)
}
{ 
    "_id" : ObjectId("60bb4cdb4a802722d8b0de11"), 
    "username" : "matan", 
    "text" : "Hello World", 
    "__v" : NumberInt(0)
}
{ 
    "_id" : ObjectId("60bb4ce14a802722d8b0de12"), 
    "username" : "System", 
    "text" : "matan has left.", 
    "__v" : NumberInt(0)
}

I want to filter the data that all the System's Welcome messages will be dropped. For example, this output will be shown:

{ 
    "_id" : ObjectId("60bb4cd74a802722d8b0de0f"), 
    "username" : "System", 
    "text" : "matan has joined!", 
    "__v" : NumberInt(0)
}
{ 
    "_id" : ObjectId("60bb4cdb4a802722d8b0de11"), 
    "username" : "matan", 
    "text" : "Hello World", 
    "__v" : NumberInt(0)
}
{ 
    "_id" : ObjectId("60bb4ce14a802722d8b0de12"), 
    "username" : "System", 
    "text" : "matan has left.", 
    "__v" : NumberInt(0)
}

But instead, only this output is shown:

{
    _id: 60bb4cdb4a802722d8b0de11,
    username: 'matan',
    text: 'Hello World',
    __v: 0
}

This is my code (NodeJS):

var msgs = Msg.find({
            $and: [
                { username: {$ne: "System"} },
                { text: {$ne: "Welcome!"} }
            ]
        }, (err, retn) => console.log(retn))

Did I miss something important? Thanks for the help.




Aucun commentaire:

Enregistrer un commentaire