2016-07-07 70 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

相關問題