Mogodb 常用的命令

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011066470/article/details/85922867

#创建数据库

use bqjc
db.bqjc.insert({"name":"菜鸟教程"})

#创建集合

db.createCollection("mgdb_test");

#删除集合(表)
db.mycol2.drop()


#插入数据
db.db_test.insert({name: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库'
})

#批量插入
var res = db.db_test.insertMany([{"name": "ljf",description:'mongodb是一个nosql'}, {'name': 'ces',description:"nosql",_id:'123sd'}])
res

#全量查询
db.getCollection('db_test').find({})

#通过id查询
db.monitor_record.findOne({"_id":ObjectId("20D7977DB2562EA67872A55A1AC1DD95")});
db.webpage.find({"_id" : ("20D7977DB2562EA67872A55A1AC1DD95")})
db.getCollection('webpage_pic2.1').find({"state":-1})
#and条件查询
db.col.find({"by":"菜鸟教程", "title":"MongoDB 教程"}).pretty()

#分页查询
db.getCollection('monitor_record_finger').find({}).skip(20).limit(10)

#group by操作
db.mgdbout.aggregate([{$group : {_id: "$title", count : {$sum : 1}}}])
//相当于select title, count(title) from mycol group by title

#查询记录条数
db.getCollection('webpage_finger').find({}).count()


db.getCollection('webpage_pic2.1').find({"is_index":false}).count()


#查询全量
db.getCollection('db_test').find({})

#查询不含有url的数据
db.db_test.find( { "url": { $exists: false } } )
#查询含有url的数据
db.db_test.find( { "url": { $exists: true } } )
#模糊查询

db.getCollection('webpage_wechat').find({"title":{$regex:/不占熟人便宜,是人际交往中的最高修养/}})

#and查询
db.getCollection('webpage_wechat').find({"wx_alias" : "hey-stone","publish_time":/2018-11-15/})


db.getCollection('webpage_wechat').find({"wx_alias" : "tiexuejunshi","publish_time":/2018-12-10/})


#修改操作
db.db_test.update({'name':'ces'},{$set:{'name':'hadoopDB'}})
#批量修改一个表中所有数据某一个字段的值
db.monitor_record.update({'sid':'yqjc00000000002'},{$set:{'sid':'18140220'}},{multi:true})


#删除操作
db.db_test.remove({'name':'hadoopDB'})


都看到这里了,就顺手点击左上角的【关注】按钮,点击右上角的小手,给个评论,关注一下,再走呗!☺

猜你喜欢

转载自blog.csdn.net/u011066470/article/details/85922867