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

Write a service using node to provide an interface for the caller to encapsulate the parameters after passing in the parameters, and insert a record in mongo , 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, the a and b fields in data can be updated successfully, but the c field is not inserted. At first I thought it was because the mongodb package used was out of date. Now I have updated to the latest version 2.2.26 and the version still cannot be updated. Later, the findOneAndUpdate command was used instead, and the updated document was returned. The returned result had a c value, but there was still no c field in mongo value. 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