Tag:前端node.jsmongodmongoosemongodb
Article From:https://segmentfault.com/q/1010000012136564
Question:

var MongoClient = require('mongodb').MongoClient;

function _connectDB(callback) {
    var url = "MongoDB://127.0.0.1/haha";
    MongoClient.connect(url, function (err,db) {
        if(err) {
            callback(err,null);
            return;
        }
        callback(err,db);
    });
}

exports.updateMany = function (collectionName,json1,json2,callback) {
    _connectDB(function (err,db) {
        db.collection('collectionName').updateMany(
            json1,
            json2,
            function(err,results) {
                callback(err,results);
                db.close();//close database});});};
//limit,skipEfficient practiceVar express = require ("express");Var app = Express ();Var DB = require ("./model/db3.js");APp.get ("/xiugai", function (req, RES) {/ / notice that this is a stringDb.updateMany ("Student",{Name: "ha ha"},{$set: {name: "hee hee"}},FunCtion (err, result) {If (ERR) {Console.log (ERR);Db.close (); / / close the database}Res.send (result);});});App.listen (3000);

Result:

Answer 0:

You look at itupdateMany Method

The collection name is written dead, not the name of the collection you imported, instead of the collection name variable you imported.

Answer 1:

Change the Chinese characters in the code to unicode and try it again

Leave a Reply

Your email address will not be published. Required fields are marked *