• 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 }
  • 相关阅读:
    PHP Document 注释标记及规范 && PHP命名规范
    JavaScript 最佳实践
    PHP正则表达式详解(三)
    $_SERVER["SCRIPT_NAME"]、$_SERVER["PHP_SELF"]、$_SERVER["QUERY_STRING"]、$_SERVER["REQUEST_URI"]
    PHP判断远程文件是否存在
    HDOJ1251-统计难题(trie树入门)
    Spark的日志配置
    实现Android 动态载入APK(Fragment or Activity实现)
    OC与JS互相调用
    mac os使用lsusb命令和连接未知的Android设备
  • 原文地址:https://www.cnblogs.com/xiaolang8762400/p/6935459.html
Copyright © 2020-2023  润新知