简体   繁体   中英

How to print updated documents of Mongodb in my console using nodejs?

I am reading a csv file if id matches it will update documents in my mongodb.But when i am printing the updated documents all are printing.below is the code.

const Account = require('./Account'); 
const parse = require('csv-parse');
const fastcsv = require("fast-csv");
const json2csv = require('json2csv').parse;
var csvWriter = require('csv-write-stream')
var newColumnValue = 'insertsuccessfully'; 
var writer = csvWriter()
let csvData = [];  
var csvData1 = [];
const fs = require('fs');

var name = [];
fs.createReadStream(__dirname + '/node_insert_2_1.csv').pipe(
parse({
    delimiter:',',
    from_line: 2
})
 )
     .on('data',function(dataRow) {
      var customerid = dataRow[1];
       
        Account.findOne({customerId: customerid}).then(
        function(doc)
        {
            if(doc)
            {
                const updatedata =
                 {};

                 updatedata.InternalId=dataRow[0],
                 updatedata.firstname=dataRow[2],
                 updatedata.lastname =dataRow[3],
                 updatedata.email=dataRow[4],
                 
                 updatedata.phone=dataRow[5],
                 updatedata.mobilePhone =dataRow[6],
                 updatedata.primary=dataRow[7]
                const docid = doc._id;
                Account.findByIdAndUpdate(docid,updatedata,{upsert:true}).then(
                             function(doc3)
                             {
                               
                              
                                
                               console.log("Updated User : ", doc3); 
                           
                             }
                               
                        );
            
           
              
             
   
             
             console.log(docid)
             
             }

As i am printing console.log("Updated User: ", doc3); all data of csv is printing inform of json. But i need only updated data to be printed into my console

The findByIdAndUpdate function will return the entire document, not just the updates.

If there is no error, it is safe to assume that the update was successful, so you could just print the updatedata object?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM