Mongo DB——随笔
查询:
select * from 0922test;
db.getCollection('0922test').find();
select * from 0922test where name='0922test';
db.getCollection('0922test').find({'name':'0922test'});
select * from 0922test where age>30;
db.getCollection('0922test').find({'age':{$gt:"30"}}).pretty();
select * from 0922test where name='aaa' and age>30;
db.getCollection('0922test').find({'name':'aaa','age':{$gt:"30"}}).pretty();
> $gt
>= $gte
< $lt
<= $lte
!= $ne
select * from 0922test where age>30 and name = 'aaa' or name = 'bbb';
db.getCollection('0922test').find({{'age':{$gt:"30"}},$or:[{'name':'aaa'},{'name':bbb'}]}).pretty();
select name from 0922test;
db.getCollection('0922test').find({},{'name':1});
删除集合:
db.getCollection('0922test').drop();
删除文档:
query :(可选)删除的文档的条件。
justOne : (可选)如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。
writeConcern :(可选)抛出异常的级别。
delete from 0922test where name is null;
db.getCollection("0922test").remove({'name':{$exists: true}});
插入数据:
db.getCollection("0922test").insert( {
title: "0923",
description: "MongoDB",
by: "菜出差程点多",
url: "http://www.runoob.com",
tags: [
"mongodb",
"database",
"NoSQL"
],
likes: 100
} );
db.getCollection("0922test").insert( {
name: "gggh",
title: "0923"
} );
更新:
query : update的查询条件,类似sql update查询内where后面的。
update : update的对象和一些更新的操作符(如
,
,
,inc…)等,也可以理解为sql update查询内set后面的
upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
writeConcern :可选,抛出异常的级别。
update 0922test set title ='0923';
db.getCollection("0922test").update({},{$set :{'title':'0923'}}, {multi:true});