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

前端之家收集整理的这篇文章主要介绍了JS工具:mongodb Payload Too Large 与 使用教程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  • 问题

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

Try to apply this in your app.js instead.

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

Hope this help!

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

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

  1. > db.user.insert
  2. function (obj,_allow_dot) {
  3. if (!obj) {
  4. throw "no object passed to insert!";
  5. }
  6. if (!_allow_dot) {
  7. this._validateForStorage(obj);
  8. }
  9. if (typeof obj._id == "undefined" && !Array.isArray(obj)) {
  10. var tmp = obj;
  11. obj = {_id:new ObjectId};
  12. for (var key in tmp) {
  13. obj[key] = tmp[key];
  14. }
  15. }
  16. this._db._initExtraInfo();
  17. this._mongo.insert(this._fullName,obj);
  18. this._lastID = obj._id;
  19. this._db._getExtraInfo("Inserted");
  20. }
  21. > db.user.save
  22. function (obj) {
  23. if (obj == null || typeof obj == "undefined") {
  24. throw "can't save a null";
  25. }
  26. if (typeof obj == "number" || typeof obj == "string") {
  27. throw "can't save a number or string";
  28. }
  29. if (typeof obj._id == "undefined") {
  30. obj._id = new ObjectId;
  31. return this.insert(obj);
  32. } else {
  33. return this.update({_id:obj._id},obj,true);
  34. }
  35. }

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

  1. if (typeof obj._id == "undefined") {
  2. obj._id = new ObjectId;
  3. return this.insert(obj);
  4. } else {
  5. return this.update({_id:obj._id},true);
  6. }

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

2.$set

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

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

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

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

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

猜你在找的MongoDB相关文章