I don't understand why this is not working.
db.UserConnected.find( {} )
returns
{ "_id" : ObjectId("5ce262920cb02b4724dbdc0d"), "idUser" : null, "mode" : "Passager", "latitude" : 50, "longitude" : 60 }
So, as you can see, I've got an object in my UserConnected document.
He is inserted with the following peace of code where it's logged new user. Now what I'm trying to do is update an user already connected...but this is not working and I don't know why.
socket.on('new', function(information) {
console.log(information._id);
user = {};
//user already connected
if(information._id !== undefined){
console.log("update user");
user.id = information._id;
user.idUser = information.idUser;
user.mode = information.mode;
user.latitude = information.latitude;
user.longitude = information.longitude;
//Update the user connected here if that user exists
collection.updateOne({"_id" : information._id}, {$set:user} );
socket.emit('user_modified', user);
}else{
console.log("new user");
//nouvel user
user.idUser = information.idUser;
user.mode = information.mode;
user.latitude = information.latitude;
user.longitude = information.longitude;
collection.insertOne(user);
console.log(user);
socket.emit('user_inserted', user);
}
});
I send the data like this if I'm not connected
var information = {"idProfile" : 1, "mode" : "Passager", "latitude": 50, "longitude" : 60 };
Like this if I'm connected
var information = { "_id" : "ObjectId('5ce262920cb02b4724dbdc0d')", "idProfile" : 1, "mode" : "Passager", "latitude": 500, "longitude" : 60 };
I might miss something really simple but can't find what.
EDIT: When I try with a query he can't find a match neither
db.UserConnected.updateOne( {"_id": "ObjectId('5ce262920cb02b4724dbdc0d')" }, {$set : {"longitude": 70}} )
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }
==========================================================
I can't answer the issue I don't know why....anyway here is the solution then:
Sending the data as a simple string in the client side "5ce262920cb02b4724dbdc0d"
And then using this as a filter
{"_id" : ObjectID(information._id)}
with
var ObjectID = require('mongodb').ObjectID;