MongoDB基本语句操作

//创建集合(第二个参数为可选项)
db.createCollection("表名",{ capped : true, autoIndexId : true, size : 6142800, max : 10000 });
   *** capped ---- 固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。当该值为 true 时,必须指定 size 参数。
*** autoIndexId ----- 3.2 之后不再支持该参数。(可选)如为 true,自动在 _id 字段创建索引。默认为 false。
*** size ---- (可选)为固定集合指定一个最大值,即字节数。
*** max ---- (可选)指定固定集合中包含文档的最大数量。
//删除集合
db.getCollection("表名").drop();
//插入文档
db.getCollection("表名").insertOne({"a":3});
db.getCollection("表名").insertMany([{"b":3},{"c":4}]);
db.getCollection("表名").insert({
title: 'MongoDB 教程',description: 'MongoDB 是一个 Nosql 数据库'});
db.getCollection("表名").save({title: 'MongoDB 教程',description: 'MongoDB 是一个 Nosql 数据库'});
//save:如果 _id 主键存在则更新数据,如果不存在就插入数据。该方法新版本中已废弃,可以使用 db.collection.insertOne() 或 db.collection.replaceOne() 来代替。
//更新文档
db.getCollection("表名").update(
{ "count" : { $gt : 3 } } , { $set : { "test2" : "OK"},false,true);
***第一个参数是查询条件;第二个参数是要更新的结果;第三个参数是不存在可以插入,默认为false,不插入(可选);第四个参数是更新多条,为false只更新第一条匹配结果默认为false(可选);
//删除文档
db.getCollection("表名").remove({"title":"MongoDB教程"});
//查询文档
db.getCollection("表名").find({'tg':'TG378',date : {$lt :1608047999, $gt : 1607961600},TAG:{$in:["c1_b10_ep","c1_b11_ep"]}});//多条件查询
//条件操作符
https://www.cnblogs.com/ExMan/p/9553553.html(这里讲解的比较详细)
常用的一些:$gt(大于)、$lt(小于)、$gte(大于等于)、$lte(小于等于)、$in(值在数组中)、$or(或者)、$and(和)
//limit与skip方法
db.getCollection.find().limit(2);
db.getCollection.find().limit(1).skip(1);//只会显示第二条数据,跳过第一条数据了
 

 

posted @ 2020-12-21 17:21  thomas张  阅读(325)  评论(0编辑  收藏  举报