【MongoDB】继续在简单操作的边缘试探D2

1,查询

> db.things.find();
{ "_id" : ObjectId("5ac26849bc00b738b1551605"), "name" : "mongo" }
{ "_id" : ObjectId("5ac2685abc00b738b1551606"), "x" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b1551607"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5ac26989bc00b738b1551608"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5ac26989bc00b738b1551609"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b155160a"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5ac26989bc00b738b155160b"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5ac26989bc00b738b155160c"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5ac26989bc00b738b155160d"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5ac26989bc00b738b155160e"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5ac26989bc00b738b155160f"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("5ac281db1a0b224b76bfad79"), "name" : "mongo" }
{ "_id" : ObjectId("5ac281ec1a0b224b76bfad7a"), "x" : 3 }
> var cursor = db.things.find();
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("5ac26849bc00b738b1551605"), "name" : "mongo" }
{ "_id" : ObjectId("5ac2685abc00b738b1551606"), "x" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b1551607"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5ac26989bc00b738b1551608"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5ac26989bc00b738b1551609"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b155160a"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5ac26989bc00b738b155160b"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5ac26989bc00b738b155160c"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5ac26989bc00b738b155160d"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5ac26989bc00b738b155160e"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5ac26989bc00b738b155160f"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("5ac281db1a0b224b76bfad79"), "name" : "mongo" }
{ "_id" : ObjectId("5ac281ec1a0b224b76bfad7a"), "x" : 3 }
> 

上面的例子显示了游标风格的法代输出。hasNext函数告诉我们是否还有数据,如果有则可以调用next函数。当使用的是JavaScript Shell可以用到JS 的特性, forEach就可以输出游标了。

下面的例子就是使用forEach来循环输出: forEach必须定义一个函数供每个游标元素调用。

> db.things.find().forEach(printjson);
{ "_id" : ObjectId("5ac26849bc00b738b1551605"), "name" : "mongo" }
{ "_id" : ObjectId("5ac2685abc00b738b1551606"), "x" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b1551607"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5ac26989bc00b738b1551608"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5ac26989bc00b738b1551609"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b155160a"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5ac26989bc00b738b155160b"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5ac26989bc00b738b155160c"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5ac26989bc00b738b155160d"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5ac26989bc00b738b155160e"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5ac26989bc00b738b155160f"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("5ac281db1a0b224b76bfad79"), "name" : "mongo" }
{ "_id" : ObjectId("5ac281ec1a0b224b76bfad7a"), "x" : 3 }
> 

使用游标的时候请注意占用内存的问题,特别是很大的游标对象,有可能会内存溢出。所以应该用迭代的方式来输出,下面的示例则是把游标转换成真实的数组类型:

 

> var arr = ab.things.find().toArray();
2018-04-03T03:32:55.457+0800 E QUERY    [thread1] ReferenceError: ab is not defined :
@(shell):1:9
> var arr = db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("5ac26989bc00b738b155160a"), "x" : 4, "j" : 4 }
> 

2,条件查询

//类似于SELECT * FROM things WHERE name="mongo"

> db.things.find({name:"mongo"}).forEach(printjson);
{ "_id" : ObjectId("5ac26849bc00b738b1551605"), "name" : "mongo" }
{ "_id" : ObjectId("5ac281db1a0b224b76bfad79"), "name" : "mongo" }
> 

//类似于SELECT * FROM things WHERE x=4

> db.things.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("5ac26989bc00b738b1551607"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5ac26989bc00b738b1551608"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5ac26989bc00b738b1551609"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5ac26989bc00b738b155160a"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5ac26989bc00b738b155160b"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5ac26989bc00b738b155160c"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5ac26989bc00b738b155160d"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5ac26989bc00b738b155160e"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5ac26989bc00b738b155160f"), "x" : 4, "j" : 9 }
> 

 

3,避免游标的查询

 

为了方便考虑, MongoDB Shell 避免游标可能带来的开销,提供了一个findOne()函数。这个函数和find()函数一样,不过它返回的是游标里第一条数据,或者返回null ,即空数据。作为一个例子, name=" mongo" 可以用很多方法来实现,可以用next()来循环游标或者当做数组返回第一个元素。但是用findOne()方法则更简单和高效:

> printjson(db.things.findOne({name:"mongo"}));
{ "_id" : ObjectId("5ac26849bc00b738b1551605"), "name" : "mongo" }
> 

 

这里先打住了,困死了

 

其他查询马克一下

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(MongoDB)