$set is not working.

96 views
Skip to first unread message

John Lim

unread,
Dec 27, 2015, 4:54:14 AM12/27/15
to mongodb-user
hi guys i'm trying to $set a field in my document from false to true, it is working on other collection however the same query isn't working on this one. 

let's say i have a example document look like this. 


{


        "_id" : ObjectId("565d53893454a8520659be84"),


        "customer_name" : "john",


        "customer_gender" : "male",


        "customer_age" : "20",


        "customer_uuid" : "1231231232334342123123",


        "customer_date_joined" : ISODate("2015-12-01T08:00:09.739Z"),


        "customer_claimed_deals" : [ ],


        "customer_preferences" : [


                "Dim sum"


        ],


        "__v" : 0,


        "ban_status" : false


}


and what i wanted to do is to set the "ban_status" to true, so i did this 

  this.banCustomer = function(req, res, next){
    var data = req.body;
    Customer.update({"_id":data.customer_id},{$set:{"ban_status":true}},function(err,customers){
      if(err){
        res.sendStatus(500);
      }
      return res.sendStatus(200);
    });
  };

so this function have no problem i have tested it, i can get the customer_id and everything is working fine. BUT when this query is executed i get 

console.log(customers);
{ ok: 0, n: 0, nModified: 0 }


can anyone tell me what i did wrong?



Tim Hawkins

unread,
Dec 27, 2015, 4:58:23 AM12/27/15
to mongodb-user

Double check that data.customer._id  is a mongoid type, and not a string.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/7aab8308-6f34-4399-9e9a-5c64ba7bc32c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Lim

unread,
Dec 27, 2015, 5:17:33 AM12/27/15
to mongodb-user
Hi Tim, 

i checked, it is a string but i have been using string to query all time it has no problem. 
however, is there a way to convert the string to mongoid type?

Tim Hawkins

unread,
Dec 27, 2015, 5:25:03 AM12/27/15
to mongodb-user

Im not sure what your language is there, but in php we would just do
new MongoId($data->customer->_id).

In javascript (mongo shell) it would be

ObjectId(data.customer._id)

John Lim

unread,
Dec 27, 2015, 5:44:45 AM12/27/15
to mongodb-user
okay i have try with objectId but i get the same result. 
i try to find by using the string objectId, it works so i don't think the problem is on the string obhectId.

Kevin Adistambha

unread,
Dec 29, 2015, 1:12:24 AM12/29/15
to mongodb-user

Hi John,

I believe Tim is correct in concluding that the reason the update failed is due to the datatype of the _id field. Although commonly represented as a 24-digit hex string for display purposes, an ObjectID is actually stored as a 12-byte value.

Assuming you’re using the official Node driver and the type of data.customer_id is a String, the code you provided could be modified as such:

var ObjectID = require('mongodb').ObjectID;
...
Customer.update(
{"_id":new ObjectID(data.customer_id)}
,{$set:{"ban_status":true}},function(err,customers){
...

Also, please note that the use of update() is deprecated in the official Node 2.0 driver in favor of updateOne() and updateMany().

For more information see the Node.js driver documentation.

If you’re still having difficulty, can you please provide a code snippet and let us know the specific version of the Node.js driver that you are using.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages