Nodejs - mongodb - how to find everything where a! = B?

These are 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 entries where the channel is not equal to 3, below is what I tried

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

problem: the result gives me the whole entry where channel = 3. This is wrong.

+3
source share
1 answer

Try

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

Because each item in the session collection contains a session object that contains the channel attribute.

+9
source

All Articles