首页 > 代码库 > Mongodb中的js语法

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 }

Mongodb中的js语法