2016-07-07 97 views
1

我的变量ID中包含对象标识,但是当我运行此updateOne查询时,它不会更新数据库中的文档。当我更换updateOne的updateOne参数({})它不恰当地更新数据库中的第一条记录,所以我知道我的一套语法是正确的......所以这个问题必须在这行代码:Mongodb查询在nodejs中不起作用。为什么这段代码不工作?

{"_id": "ObjectId(\""+id+"\")"}}, 

但是当我用console.log打印这行代码时,它看起来与mongoDB查询相同。任何人都可以帮我弄清楚为什么这条线不起作用?是否存在类型转换问题或类似问题?

   db.collection('profile').updateOne(
       {"_id": "ObjectId(\""+id+"\")"}}, 
       { $set: 
        { 
         "star_rating": updatedProfile.test , 
         "address.street": updatedProfile.street, 
         "address.city": updatedProfile.city, 
         "address.postalcode": updatedProfile.postal, 
         "address.country": updatedProfile.country, 
         "phonenumber": updatedProfile.phone, 
         "birthday": updatedProfile.datepicker 
        } 
       }, //set 
       { 
       upsert: false //do not create a doc if the id doesn't exist 
      } 
      ); //updateOne 

回答

0

能否请您试试这个,看看它的工作原理:

var ObjectID = require('mongodb').ObjectID, 

db.collection('profile').updateOne(
    {"_id": new ObjectID(id)}}, 
    { $set: 
     { 
      "star_rating": updatedProfile.test , 
      "address.street": updatedProfile.street, 
      "address.city": updatedProfile.city, 
      "address.postalcode": updatedProfile.postal, 
      "address.country": updatedProfile.country, 
      "phonenumber": updatedProfile.phone, 
      "birthday": updatedProfile.datepicker 
     } 
    }, //set 
    { 
    upsert: false //do not create a doc if the id doesn't exist 
}); //updateOne 
+0

非常感谢你,蒂姆。它的工作=) – yss

+0

@yss不用担心它为你工作。 – Tim