node.js - mongo insert a record but some data is not inserted
天蓬老师
天蓬老师 2017-05-16 13:39:50
0
2
446

Write a service usingnodeto provide an interface for the caller to encapsulate the parameters after passing in the parameters, andinserta record inmongo, now it is found that some fields cannot be updated. The code is similar to the following

const db = mongodb.collection('collection_name'); let data = { a: 'value1', b: 'value2', c: 'value3' }; db.insert(data);

For example, theaandbfields indatacan be updated successfully, but thecfield is not inserted. At first I thought it was because themongodbpackage used was out of date. Now I have updated to the latest version2.2.26and the version still cannot be updated. Later, thefindOneAndUpdatecommand was used instead, and the updated document was returned. The returned result had acvalue, but there was still nocfield inmongovalue. Ask God for answers! ! !

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (2)
PHPzhong

Try changing the save method without passing the _id value.

    迷茫

    I simply tried it according to your statement, but I didn’t find what you said.

    var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { var collection = db.collection("collection_name"); var data = { a: 'value1', b: 'value2', c: 'value3' }; collection.insert(data); });

    Software information: Database MongoDB 3.4 + driver mongodb 2.2.26

    For reference.

    Love MongoDB! Have fun!

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!