Home > Database > Mysql Tutorial > MongoDB插入查询数据

MongoDB插入查询数据

WBOY
Release: 2016-06-07 17:56:33
Original
1041 people have browsed it

插入: use keke 自动在写入数据后产生keke数据库。 j = { name : "mongo" }; {"name" : "mongo" } t = { x : 3 }; { "x" : 3 } db.things.save(j); db.things.save(t); 查询 db.things.find();{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "m

插入:

use keke

自动在写入数据后产生keke数据库。

> j = { name : "mongo" }; {"name" : "mongo"} > t = { x : 3 }; { "x" : 3 } > db.things.save(j); > db.things.save(t);

查询

> db.things.find(); { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" } { "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }

> var cursor = db.things.find(); > while (cursor.hasNext()) printjson(cursor.next()); { "_id" : ObjectId("501c7e5cab2f3c46faf8e68a"), "name" : "mongo" } { "_id" : ObjectId("501c7e65ab2f3c46faf8e68b"), "x" : 3 }

forEach() 必须定义一个函数供每个游标元素调用.forEavh注意大小写,香港空间,javascript区分大小写。

> db.things.find().forEach(printjson); { "_id" : ObjectId("501c7e5cab2f3c46faf8e68a"), "name" : "mongo" } { "_id" : ObjectId("501c7e65ab2f3c46faf8e68b"), "x" : 3 }

我们也可以把游标当作数组来用:

 

> var cursor = db.things.find(); > printjson(cursor[1]); { "_id" : ObjectId("501c7e65ab2f3c46faf8e68b"), "x" : 3 } >

 

下面的示例则是把游标转换成真实的数组类型:

> var arr = db.things.find().toArray(); > arr[1] { "_id" : ObjectId("501c7e65ab2f3c46faf8e68b"), "x" : 3 } >

 

 

 

,美国服务器,香港服务器租用
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template