search

Home  >  Q&A  >  body text

node.js - mongo insert a record but some data is not inserted

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! ! !

天蓬老师天蓬老师2783 days ago562

reply all(2)I'll reply

  • PHPz

    PHPz2017-05-16 13:41:50

    Try changing the save method without passing the _id value.

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:41:50

    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!

    reply
    0
  • Cancelreply