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.

link|edit|flag

1  
Did you try having 3 as a string. var k = '3' – Donnie H 5 hours ago
i tried that, doesn't work. – runrunforest 5 hours ago

1 Answer

up vote 1 down vote accepted

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".

link|flag
that gives all the result where channel = k. – runrunforest 1 hour ago
1  
That's because "3" is stored as a string. You should try {'$ne':'3'}. – FGRibreau 1 hour ago

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.