了解db.collection.find()

db.collection.find()可能是mongodb中最常用的方法之一了,其定义为db.collection.find(query, projection), 即查询一个collection中的文档并返回一个包含了选定文件(document)的字段(field)的游标。

 

参数   query : 是可选参数,基于查询操作符指定了查询的条件,若希望返回collection中的所有文件,则无需指定该query 参数,直接 db.collection.find()即可

参数 projection:是可选参数,指定了那些字段是需要返回的,若要返回所有字段则不必要指定该参数。

 

projection参数的形式如下:

{ field1: <boolean>, field2: <boolean> ... }


如上boolean布尔类型可以是下面的值:

  • 1 或者 true 代表包含该field。find()方法总是包括_id字段,即便你这里没有指定要_id字段。
  • 0或者false 代表不不包含该field。

 

例如:

> db.dbdao_student.insert({name:"maclean",age:30,score:100});
WriteResult({ "nInserted" : 1 })
> db.dbdao_student.insert({name:"jiang",age:20,score:100});
WriteResult({ "nInserted" : 1 })
>
>
>
> db.dbdao_student.find({},{age:1});
{ "_id" : ObjectId("55461b72fefe5936ccc41386"), "age" : 30 }
{ "_id" : ObjectId("55461b7cfefe5936ccc41387"), "age" : 20 }

一个更复杂一些的例子:

 

对于给定的collection, tab1

> db.dbdao_tab1.insert({a:0,b:0,c:0});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:0,b:0,c:0});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:1,b:1,c:1});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:1,b:2,c:1});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:2,b:2,c:2});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:2,b:2,c:2});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:3,b:3,c:3});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:3,b:3,c:3});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:4,b:4,c:4});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:4,b:4,c:4});
WriteResult({ "nInserted" : 1 })

> db.dbdao_tab1.find()
{ "_id" : ObjectId("554727b8ce7f1dc05695adad"), "a" : 0, "b" : 0, "c" : 0 }
{ "_id" : ObjectId("554727bdce7f1dc05695adae"), "a" : 0, "b" : 0, "c" : 0 }
{ "_id" : ObjectId("554727c7ce7f1dc05695adaf"), "a" : 1, "b" : 1, "c" : 1 }
{ "_id" : ObjectId("554727ccce7f1dc05695adb0"), "a" : 1, "b" : 2, "c" : 1 }
{ "_id" : ObjectId("554727d6ce7f1dc05695adb1"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727dfce7f1dc05695adb2"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727e7ce7f1dc05695adb3"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727eece7f1dc05695adb4"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727f6ce7f1dc05695adb5"), "a" : 4, "b" : 4, "c" : 4 }
{ "_id" : ObjectId("554727fbce7f1dc05695adb6"), "a" : 4, "b" : 4, "c" : 4 }

对于查询db.dbdao_tab1.find( {a:1,b:2} , {a:1} ),会返回多少个document

> db.dbdao_tab1.find( {a:1,b:2} , {a:1} ); 
{ "_id" : ObjectId("554727ccce7f1dc05695adb0"), "a" : 1 }


 

Screen Shot 2015-05-04 at 4.12.18 PM

例子

返回一个collection中的所有document

find()方法不加任何参数即从一个collection中返回所有的document 包括了所有的field。 举例来说,下面的操作返回了dbdao中所有的文档:

db.dbdao.find()

查找符合匹配查询条件的document

如下面的例子查找dbdao_products 中qty > 25的document

db.dbdao_products.find( { qty: { $gt: 25 } } )

 

> db.dbdao_tab1.find( { a : { $gt :1} } );
{ "_id" : ObjectId("554727d6ce7f1dc05695adb1"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727dfce7f1dc05695adb2"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727e7ce7f1dc05695adb3"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727eece7f1dc05695adb4"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727f6ce7f1dc05695adb5"), "a" : 4, "b" : 4, "c" : 4 }
{ "_id" : ObjectId("554727fbce7f1dc05695adb6"), "a" : 4, "b" : 4, "c" : 4 }
> 

> db.dbdao_tab1.find( { a : { $in : [ 1 ,2 ] } });
{ "_id" : ObjectId("554727c7ce7f1dc05695adaf"), "a" : 1, "b" : 1, "c" : 1 }
{ "_id" : ObjectId("554727ccce7f1dc05695adb0"), "a" : 1, "b" : 2, "c" : 1 }
{ "_id" : ObjectId("554727d6ce7f1dc05695adb1"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727dfce7f1dc05695adb2"), "a" : 2, "b" : 2, "c" : 2 }

 

使用操作符的查询

下面的查询返回dbdao_tab3集合中_id=5或者或者为ObjectId(“5547301bdda6d7285bef03f7”)的记录

 

> db.dbdao_tab3.find();
{ "_id" : 432424, "a" : 1 }
{ "_id" : ObjectId("5547301bdda6d7285bef03f7"), "a" : 10 }

> db.dbdao_tab3.find(
... { _id : { $in : [432424,ObjectId("5547301bdda6d7285bef03f7") ] }
... }
... )
{ "_id" : 432424, "a" : 1 }
{ "_id" : ObjectId("5547301bdda6d7285bef03f7"), "a" : 10 }




 

范围查询

组合使用比较操作符来实现范围查询,下面的操作会返回field在value1 和 value2之间的document:

db.collection.find( { field: { $gt: value1, $lt: value2 } } );

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *