MongoDB 一些记录

Queries in MongoDB:
db.collection.find()
db.collection.findOne()
db.collection.find( <query>, <projection> )
db.collection.findOne( <query>, <projection> )

Operators:
$or, $lt, $gt, $in, dot notation, $elemMatch

Query Document:

db.collection.find( {} )
db.collection.find()
db.collection.find( { type: "snacks" } )
db.collection.find( { type: { $in: [ 'food', 'snacks' ] } } )
db.collection.find( { type: 'food', price: { $lt: 9.95 } } )
db.collection.find( { $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ] } )
db.collection.find( { type: 'food', $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ] } )

Subdocuments:

db.collection.find({ producer: { company: "ABC123", address : "123 Street" }})
db.collection.find( { 'producer.company': 'ABC123' } )

Arrays:
db.collection.find( { tags: [ 'fruit', 'food', 'citrus' ] } )
the query matches all documents where the value of the field tags is an array and holds three elements, 'fruit', 'food', and 'citrus', in this order
db.collection.find( { tags: 'fruit' } )
the query matches all documents where the value of the field tags is an array that contains, as one of its elements, the element 'fruit'
db.collection.find( { 'tags.0' : 'fruit' } )
the query uses the dot notation to match all documents where the value of the tags field is an array whose first element equals 'fruit'
db.collection.find( { 'memos.0.by': 'shipping' } )
selects all documents where the memos contains an array whose first element (i.e. index is 0) is a subdocument with the field by with the value 'shipping'
db.collection.find( {'memos.by' ; 'shipping'} )
selects all documents where the memos field contains an array that contains at least one subdocument with the field by with the value 'shipping'
db.collection.find( { 'memos.memo': 'on time', 'memos.by': 'shipping' } )
query for documents where the value of the memos field is an array that has at least one subdocument that contains the field memo equal to 'on time' and the field by equal to 'shipping'
db.collection.find({memos : { $elemMatch : { memo : 'on time', by : 'shipping'}}})
same as above, just use "$elemMatch" operator

===============
文档 Document 集合 Collection 子集合 blog.users blog.contents 数据库: DB // shell: show dbs; use mydb; show collections; db.mycollection;

db.foo.insert({"name":"yy"}); // 每条插入的记录都会多一个属性 "_id", 每个集合的每个document对象都有唯一的"_id"。

批量插入:插入 document数组 到一个collection中。
mongoimport 工具: 导入原始数据,从数据feed或者mysql导入。
删除文档:db.foo.remove(); //只是删除集合foo的文档,不删除集合本身,也不删除集合的索引。
db.foo.drop(); //删除整个foo集合。删除所有数据的话,这个更快。
db.foo.update(queryCnd, newDoc); //文档替换

//数据的更新: . . . . . .

//数据查询:
db.user.find() == db.user.find({})
db.user,.find({"name":"joe", "age":30}); //多条件联合查询
//指定返回的键,即映射, 无论如何默认_id总是返回,除非排除他 "_id":0
db.user.find({}, {"name":1,"age":1});
db.user.find({}, {"age":0});
db.user.find({}, {"name":1,"age":0});不允许,这个不能混用,但是 db.user.find({}, {"name":1,"_id":0});允许,_id好特殊啊

db.user.find({"age":{"$gte":18, "lte":30}}); //$gte: great than and equal, $lte: less than and equal
db.user.find( {"userId" : {"$in" : ["joe", 123]} }); // find user whose userid is "joe" or 123
db.raffle.find({"$or" : [{"ticket_no" : {"$in" : [390, 256, 757]} }, {"winner" : true}]}); // this is or usage

// 使用and查询,第一个条件要尽量排除记录, 使用or,第一个条件要尽量匹配记录,这样效率才高。 mongodb是从左往右解析条件的。

db.c.find({"z" : {"$in" : [null], "$exists" : true } }); // 没有$eq操作符 ,所以用$in了

db.user.find({"name" : /joe/i}); //查找名字为"Joe"(不区分大小写)的用户, 也可以匹配/joe/i 本身。

// $where 查询, 效率问题, 另外一种复杂查询是 MapReduce方式
db.foo.insert({"apple" : 1, "banana" : 6, "peach" : 3});
db.foo.insert({"apple" : 1, "spinach" : 6, "peach" : 3});

db.foo.find({"$where" : function(){
for(var cur in this){
for(var oth in this){
if(cur!=oth && this[cur] == this[other]){
return true;
}
}
}
return false;
}});

// 游标 limit | skip | sort使用
var cursor = db.user.find();
var item = null;
while(cursor.hasNext()){
item = cursor.next();
// do stuff with item
}

//skip 忽略大量数据是很慢滴
var cursor = db.user.find({"status" : 1}).sort({"name" : 1, "age" : -1}).limit(10).skip(30)
//游标默认超时==>>释放资源 //自定义超时时间

//缩影Index 用来加速查询
db.user.ensureIndex({"name" : 1}) //name 的升序索引, 排序影响索引的缓存,尽量使的update/delete不会引起索引缓存的剧烈变化
//索引缺点: 每次插入、更新、和删除都会产生额外的开销,用于维持索引顺序。
//索引内嵌文档的键
db.user.ensureIndex("comments.date" : 1);

//为排序创建索引,如果排序字段无索引,且数量巨大,内存排序无法实现,mongodb可能会报错
db.user.find(...).sort({"name" : 1});
db.user.ensureIndex({"name" : 1});

//Index Name, 默认使用key1_sort1_key2_sort2..._keyN_sortN
db.user.ensureIndex({...}, {"name" : "userInamendex"})

// 唯一性约束, unique index
db.user.ensureIndex({"name" : 1}, {"unique" : true});
db.user.ensureIndex({"name" : 1, "status" : 1}, {"unique" : true});

//查询 删除索引
db.user.getIndexes();
db.user.dropIndex(indexName);

//聚合
db.user.count();
db.user.count("status" : 1);
db.user.distinct("name"); //如果用distinct只能distinct一个字段

//MapReduce
emit(key, value) //按照key来分类,reduce 按照value,做出结果
db.bk.mapReduce(map,reduce,...);

//~~~~~~~~~~~~~~~~~~~~~~~
数据库命令 shell命令 db.runCommand(...);
固定集合,类相似先进先出列表的序列化

原文地址:https://www.cnblogs.com/amosleaf/p/3109813.html