一、查询sql
【基本形式】db.col.find().pretty():pretty() 方法以格式化的方式来显示全部文档。
mongodb
【获取记录条数】:db.users.find().count();
数据库
【读取指定数量的数据 limit(number)】:db.COLLECTION_NAME.find().limit(NUMBER)
【排序 1-asc;-1-desc】:db.COLLECTION_NAME.find().sort({KEY:1})
db.col.find({},{"title":1,_id:0}).sort({"likes":-1})
【count(*) 聚合框架】:MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。
eg:经过字段by_user字段对数据进行分组,并计算by_user字段相同值的总和
select by_user as _id, count(*) as num_tutorial from mycol group by by_user
(1)分组求和:db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
结果以下:
{ "result" : [ { "_id" : "runoob.com", "num_tutorial" : 2 }, { "_id" : "Neo4j", "num_tutorial" : 1 } ], "ok" : 1 }
(2)分组求平均框架
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])【根据 by_user 字段分组,每组计算 likes 的平均值】url
(3)计算每组的极值:spa
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
code
【跳过指定数量的数据】:db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) 对象
>db.col.find({},{"title":1,_id:0}).limit(1).skip(1)【总共读取1条,但跳过1条,因此读的是第 2 条数据】
(4)聚合管道

db.articles.aggregate( [ { $match : { score : { $gt : 70, $lte : 90 } } }, { $group: { _id: null, count: { $sum: 1 } } } ] );
$match用于获取分数大于70小于或等于90记录,而后将符合条件的记录送到下一阶段$group管道操做符进行处理。
blog
db.article.aggregate( { $project : { _id : 0 ,【不包含这个默认的字段】 title : 1 , author : 1 }});
这样的话结果中就只还有tilte和author 两个字段了,默认状况下_id字段是被包含的,若是要想不包含_id话能够:_id:0排序
【条件查询】

(3)多条件查询 And
db.col.find({key1:value1, key2:value2}).pretty()
db.col.find({"by":"菜鸟教程", "title":"MongoDB 教程"}).pretty()
以上实例中相似于 WHERE 语句:WHERE by='菜鸟教程' AND title='MongoDB 教程'
(4)Or 查询
db.tableName.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
>db.col.find({$or:[{"by":"菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
(5)AND 和 OR 联合使用
相似常规 SQL 语句为: 'where likes>50 AND (by = '菜鸟教程' OR title = 'MongoDB 教程')'
>db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty()
{ "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
(6) 根据 mongo 中属性的类型查询数据
若是想获取 "col" 集合中 title 为 String 的数据
db.col.find({"title" : {$type : 2}})
Double:1;String:2;Object:3;Array:4;Binary data:5;Undefined:6;Object id:7;Boolean:8;Date:9;Null:10;Regular Expression:11;JavaScript:13;Symbol:14;JavaScript(with scope):15; 32-bit integer:16;Timestamp:17;64-bit integer:18;Min kye:255(query with -1);Max key:127;
二、insert()方法
>db.col.insert({ title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })
三、update() 方法
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
参数说明:
>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) # 输出信息
所有更新:db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
四、清空集合
db.col.remove({})
五、索引