Mongodb中的js语法

  1. 定义一个变量
  2. > var len = 10;
  3. For循环 这里的db和data都可以作为对象 save是方法 接收一个临时定义的对象
  4. > for(var i = 0; i < len; i++){db.data.save({x:i})};
  5. WriteResult({ "nInserted" : 1 })
  6. > db.data.find();
  7. { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
  8. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
  9. { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
  10. { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
  11. { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
  12. { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
  13. { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
  14. { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
  15. { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
  16. { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
  17. 使用游标查询
  18. > var cur = db.data.find();
  19. > cur[1]
  20. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
  21. > printjson(cur[1])
  22. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
  23. > var cur = db.data.find();
  24. 对游标执行While循环
  25. > while(cur.hasNext()) printjson(cur.next());
  26. { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
  27. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
  28. { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
  29. { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
  30. { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
  31. { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
  32. { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
  33. { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
  34. { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
  35. { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
  36. 多么典型的js语法 直接接收一个方法
  37. > db.data.find().forEach(printjson);
  38. { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
  39. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
  40. { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
  41. { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
  42. { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
  43. { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
  44. { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
  45. { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
  46. { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
  47. { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
  48. 接收一个临时定义的带参数的方法
  49. > db.data.find().forEach(function(e){printjson(e)});
  50. { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
  51. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
  52. { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
  53. { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
  54. { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
  55. { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
  56. { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
  57. { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
  58. { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
  59. { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }

转载于:https://www.cnblogs.com/xiaolang8762400/p/6935459.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值