Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs - mongodb - how to find all where a != b?

This is collection sessions

{
    "_id": "R65i3SmvucW9imK2cxA6wdFb.GXoSHjly7obzFNslklNCBvE0UrW/qOiNmiBtPN24/1c",
    "session": {
        "channel": "all",
        "username": "xuka"
    },
    "expires": NumberLong("1307692520000")
} {
    "_id": "zJYZj2jwxa5zN0uZcCZC26zp.Tpp8fVkqwKLZEpRWgq7/3DDTcDw9VSlskBum28gox+0",
    "session": {

        "channel": "3",
        "username": "hellos"
    },
    "expires": NumberLong("1307692826000")
}

I need to find records where channel is not equal 3, below is what I've tried

var k =3;
db.collection('sessions', function(err, collection){                        
    collection.find({channel:{'$ne':k}},function(err, cursor) {     
    });
});

problem: the result gives me all the record where channel = 3. It's wrong.

like image 863
angry kiwi Avatar asked Jan 31 '26 11:01

angry kiwi


1 Answers

Try

var k =3;
db.collection('sessions', function(err, collection){                        
    collection.find({'session.channel':{'$ne':k+''}},function(err, cursor) {     
    });
});

Because each sessions collection's item contain an object "session" which contain an attribute "channel".

like image 164
FGRibreau Avatar answered Feb 03 '26 02:02

FGRibreau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!