首页 > 代码库 > Mongodb中的js语法
Mongodb中的js语法
- 定义一个变量
- > var len = 10;
- For循环 这里的db和data都可以作为对象 save是方法 接收一个临时定义的对象
- > for(var i = 0; i < len; i++){db.data.save({x:i})};
- WriteResult({ "nInserted" : 1 })
- > db.data.find();
- { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
- { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
- { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
- { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
- { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
- { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
- { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
- { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
- { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
- { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
- 使用游标查询
- > var cur = db.data.find();
- > cur[1]
- { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
- > printjson(cur[1])
- { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
- > var cur = db.data.find();
- 对游标执行While循环
- > while(cur.hasNext()) printjson(cur.next());
- { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
- { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
- { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
- { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
- { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
- { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
- { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
- { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
- { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
- { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
- 多么典型的js语法 直接接收一个方法
- > db.data.find().forEach(printjson);
- { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
- { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
- { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
- { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
- { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
- { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
- { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
- { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
- { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
- { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
- 接收一个临时定义的带参数的方法
- > db.data.find().forEach(function(e){printjson(e)});
- { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
- { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
- { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
- { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
- { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
- { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
- { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
- { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
- { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
- { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
Mongodb中的js语法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。