β

MongoDB:查询记录( Select )

PostgreSQL DBA 6 阅读
 

上篇 blog 学习了 MongoDB 的插入,接下来学习数据查询相关的内容。
MongoDB 查询有好几种方法,具体为以下:


--基础表
> db.test_2.find();
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" : "francs" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9c"), "id" : 2, "name" : "fpZhou" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9d"), "id" : 3, "name" : "tutu" }  --find 游标查询
{ "_id" : ObjectId("50a0d46deb825d827b0c3f9e"), "id" : 4, "name" : "am", "address" : "zhoushan" }


--1 使用 find 游标查询

var cursor=db.test_2.find();
while(cursor.hasNext()) printjson(cursor.next());

{
"_id" : ObjectId("50a0d46ceb825d827b0c3f9b"),
"id" : 1,
"name" : "francs"
}
{
"_id" : ObjectId("50a0d46ceb825d827b0c3f9c"),
"id" : 2,
"name" : "fpZhou"
}
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9d"), "id" : 3, "name" : "tutu" }
{
"_id" : ObjectId("50a0d46deb825d827b0c3f9e"),
"id" : 4,
"name" : "am",
"address" : "zhoushan"
}

备注:通过 while 循环,使用 next() 方法依次读取所有 document,并用 printjson 方法
显示结果。


--2 使用 find 的 forEach()查询

> db.test_2.find().forEach(printjson);
{
"_id" : ObjectId("50a0d46ceb825d827b0c3f9b"),
"id" : 1,
"name" : "francs"
}
{
"_id" : ObjectId("50a0d46ceb825d827b0c3f9c"),
"id" : 2,
"name" : "fpZhou"
}
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9d"), "id" : 3, "name" : "tutu" }
{
"_id" : ObjectId("50a0d46deb825d827b0c3f9e"),
"id" : 4,
"name" : "am",
"address" : "zhoushan"
}



--3 定制查询

MongoDB 的查询和关系型数据库的查询差异较大,但大多数据的关系型数据库的查询可以
转换成MongoDB 的查询,以下为简单的几种情况。

--3.1 select * From test_2;

> db.test_2.find();
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" : "francs" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9c"), "id" : 2, "name" : "fpZhou" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9d"), "id" : 3, "name" : "tutu" }
{ "_id" : ObjectId("50a0d46deb825d827b0c3f9e"), "id" : 4, "name" : "am", "address" : "zhoushan" }

--3.2 select id,name from test_2;

> db.test_2.find({},{id:1,name:1});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" : "francs" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9c"), "id" : 2, "name" : "fpZhou" }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9d"), "id" : 3, "name" : "tutu" }
{ "_id" : ObjectId("50a0d46deb825d827b0c3f9e"), "id" : 4, "name" : "am" }


--3.3 select id from test_2;

> db.test_2.find({},{id:1});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1 }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9c"), "id" : 2 }
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9d"), "id" : 3 }
{ "_id" : ObjectId("50a0d46deb825d827b0c3f9e"), "id" : 4 }


--3.4 select * From test_2 where id=1;

> db.test_2.find({id:1});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" : "francs" }


--3.5 select id,address from test_2 where id=4;

> db.test_2.find({address:'zhoushan'},{id:1,name:1});
{ "_id" : ObjectId("50a0d46deb825d827b0c3f9e"), "id" : 4, "name" : "am" }


--3.6 select * from test_2 where id=1 and name='francs'

> db.test_2.find({id:1,name:'francs'});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" : "francs" }

备注:先学习这么多,其它更复杂的查询以后再补充。


--4 limit 用法

> db.things.find();db.things.find();
{ "_id" : ObjectId("50a0d637eb825d827b0c3f9f"), "id" : 1, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa0"), "id" : 2, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa1"), "id" : 3, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa2"), "id" : 4, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa3"), "id" : 5, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa4"), "id" : 6, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa5"), "id" : 7, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa6"), "id" : 8, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa7"), "id" : 9, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa8"), "id" : 10, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa9"), "id" : 11, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3faa"), "id" : 12, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fab"), "id" : 13, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fac"), "id" : 14, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fad"), "id" : 15, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fae"), "id" : 16, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3faf"), "id" : 17, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb0"), "id" : 18, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb1"), "id" : 19, "name" : "aaa" }
{ "_id" : ObjectId("50a0d637eb825d827b0c3fb2"), "id" : 20, "name" : "aaa" }
Type "it" for more

> db.things.find().limit(1);
{ "_id" : ObjectId("50a0d637eb825d827b0c3f9f"), "id" : 1, "name" : "aaa" }

--5 skip 用法

> db.things.find().skip(10).limit(1);
{ "_id" : ObjectId("50a0d637eb825d827b0c3fa9"), "id" : 11, "name" : "aaa" }

--6 Sorting 用法

db.things.find().sort({id:1});

--7 附:db.collection.find
The find() method selects documents in a collection and returns a cursor to the selected documents.
The find() method takes the following parameters.

Parameters:
1 query (document) – Optional. Specifies the selection criteria using query operators. Omit
the query parameter or pass an empty document (e.g. {}) to return all documents in the collection.
2 projection (document) –
Optional. Controls the fields to return, or the projection. The projection argument will resemble the
following prototype:
{ field1: boolean, field2: boolean ... }
The boolean can take the following include or exclude values:
1 or true to include. The find() method always includes the _id field even if the field is not explicitly
stated to return in the projection parameter.
0 or false to exclude.
The projection cannot contain both include and exclude specifications except for the exclusion of the _id field.

Returns:
A cursor to the documents that match the query criteria and contain the projection fields.

--8  参考
http://docs.mongodb.org/manual/reference/sql-comparison/
http://www.mongodb.org/display/DOCS/Tutorial
http://www.mongodb.org/display/DOCS/Querying

 
作者:PostgreSQL DBA
Francs 的博客
原文地址:MongoDB:查询记录( Select ), 感谢原作者分享。

发表评论