加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JS工具:mongodb Payload Too Large 与 使用教程

发布时间:2020-12-13 12:44:44 所属栏目:百科 来源:网络整理
导读:问题 Error: 'Request entity too large'. How to increase bodyParser limit outside meanio module? 方案 Try to apply this in your app.js instead. app.use(bodyParser.json({limit: '50mb'}));app.use(bodyParser.urlencoded({limit: '50mb'})); Hope

  • 问题

Error: 'Request entity too large'. How to increase bodyParser limit outside meanio module?
  • 方案

Try to apply this in your app.js instead.

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb'}));

Hope this help!

insert和save函数到底有什么区别呢?

由上面可以看出,save函数实际就是根据参数条件,调用了insert或update函数.如果想插入的数据对象存在,insert函数会报错,而save函数是改变原来的对象;如果想插入的对象不存在,那么它们执行相同的插入操作.

> db.user.insert
function (obj,_allow_dot) {
    if (!obj) {
        throw "no object passed to insert!";
    }
    if (!_allow_dot) {
        this._validateForStorage(obj);
    }
    if (typeof obj._id == "undefined" && !Array.isArray(obj)) {
        var tmp = obj;
        obj = {_id:new ObjectId};
        for (var key in tmp) {
            obj[key] = tmp[key];
        }
    }
    this._db._initExtraInfo();
    this._mongo.insert(this._fullName,obj);
    this._lastID = obj._id;
    this._db._getExtraInfo("Inserted");
}
> db.user.save
function (obj) {
    if (obj == null || typeof obj == "undefined") {
        throw "can't save a null";
    }
    if (typeof obj == "number" || typeof obj == "string") {
        throw "can't save a number or string";
    }
    if (typeof obj._id == "undefined") {
        obj._id = new ObjectId;
        return this.insert(obj);
    } else {
        return this.update({_id:obj._id},obj,true);
    }
}

注意其中的(insert还是update,取决于obj._id == "undefined")

    if (typeof obj._id == "undefined") {
        obj._id = new ObjectId;
        return this.insert(obj);
    } else {
        return this.update({_id:obj._id},true);
    }

obj代表需要更新的对象,如果集合内部已经存在一个和obj相同的"_id"的记录,Mongodb会把obj对象替换集合内已存在的记录,如果不存在,则会插入obj对象。

2.$set

用法:{$set:{field:value}}

作用:把文档中某个字段field的值设为value

示例: 把chenzhou的年龄设为23岁

1. > db.students.find()  
2. { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"),"name" : "chenzhou","age" : 27 }  
3. > db.students.update({name:"chenzhou"},{$set:{age:23}})  
4. > db.students.find()  
5. { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"),"age" : 23 }  
6. >  

从结果可以看到,更新后年龄从27变成了23

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读