mongo数据删除和游标

时间:2024-03-28 15:35:50

数据删除

db.集合.remove(删除条件,是否只删除一个数据);
默认删多条(false)
true删除一条
db.集合.remove({}) 删除所有元素
但集合还在
db.集合.drop() 删除集合

游标
指数据可以一行行的进行操作,类似ResultSet数据处理
在mongo里是需要使用find()就可以返回游标了
对于操作返回的游标,可使用函数操作
1.判断是否有下一行数据:hasNext()
2.取当前数据: next()

var cur=db.web.find();
cur.hasNext();
cur.next();

> db.web.find();
{ "_id" : ObjectId("592c945f997b7830e7f76d05"), "lan" : "php7", "need" : [ "mysql", "js", "html", "css", "tp", "yii" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d06"), "lan" : "jav a", "need" : [ "mysql", "js", "html", "css", "oracle", "spring" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d07"), "lan" : "python", "need" : [ "mysql", "js", "html", "css", "flask" ] }
{ "_id" : ObjectId("592c945f997b7830e7f76d08"), "lan" : "PythoN", "need" : [ "mysql", "js", "html", "css", "flask", "diagno" ] }
> var cur=db.web.find();
> cur.hasNext();
true
> cur.next();
{
"_id" : ObjectId("592c945f997b7830e7f76d05"),
"lan" : "php7",
"need" : [
"mysql",
"js",
"html",
"css",
"tp",
"yii"
]
}
var cur=db.web.find();
while(cur.hasNext()){
var p=cur.next();
print(p.lan);
} > var cur=db.web.find();
> while(cur.hasNext()){
... var p=cur.next();
... print(p.lan);
... }
php7
jav a
python
PythoN

p是object
如果需要输出json
可使用printjson()

> var cur=db.web.find();
> while(cur.hasNext()){
... var p=cur.next();
... print(p);
... }
[object BSON]
[object BSON]
[object BSON]
[object BSON] var cur=db.web.find();
while(cur.hasNext()){
var p=cur.next();
printjson(p);
}