10. MongoDB操作 普通操作
4、查询:
4.1遍历集
> var cursor = db.things.find();
> while (cursor.hasNext()) { print(tojson(cursor.next())); }
4.2 方法2
> db.things.find().forEach( function(x){print(tojson(x));});
4.3、获取结果集
> var cursor = db.things.find();
> print (tojson(cursor[4]));
> var arr = db.things.find().toArray();
> arr[5];
11. MongoDB操作 普通操作
5、条件查询:
> db.things.find({name:"mongo"}).forEach(function(x) { print(tojson(x));});
等价于:
SQL:SELECT * FROM things WHERE name="mongo"
>db.things.find({x:4}, {j:true}).forEach(function(x) { print(tojson(x));});
等价于:
SQL:SELECT j FROM things WHERE x=4
12. MongoDB操作 普通操作
6、sort用法
>db.things.find({tags :'economy'}).sort({ts:-1}).limit(10);
等价于:
SQL: select * from things where 'economy' in tags order by ts DESC limit 10
7、findOne用法
> var mongo = db.things.findOne({name:"mongo"});
> print(tojson(mongo));
8、limit用法
db.things.find().limit(3);