insert自动跳过存在数据_轻松入门mongo 数据库
use data 創(chuàng)建數(shù)據(jù)庫(kù)
show dbs show databases 顯示所有數(shù)據(jù)庫(kù)
db 顯示當(dāng)前數(shù)據(jù)庫(kù)
db.dropDatabase() 刪除當(dāng)前數(shù)據(jù)庫(kù)
創(chuàng)建集合 固定集合大小為100 最大數(shù)值1000
db.createCollection('student',{capped:true,size:100,max:1000})
//顯示所有集合
show collections
//刪除集合
db.集合名.drop()
//插入集合 _id存在就報(bào)錯(cuò)
db.runoob.insert({_id:0001,'name':'hw','age':10})
//顯示集合內(nèi)容
db.runoob.find()
//顯示一條集合內(nèi)容
db.runoob.findOne()
//格式化顯示集合內(nèi)容
db.runoob.find().pretty()
//保存集合_id存在就更新
db.runoob.save({_id:0001,'name':'hw','age':10})
//更新集合
$set 指定鍵并更新 不存在則創(chuàng)建 $unset 刪除
db.runoob.update({'name':'hw'},{'name':'xz'}) //更新一條數(shù)據(jù)替換
db.runoob.update({'name':'hw'},{$set{'name':'xz'}}) //更新一條數(shù)據(jù)更新
db.runoob.update({'name':'hw'},{'name':'xz'},{multi:true}) //更新全部數(shù)據(jù)
//刪除集合數(shù)據(jù)
db.runoob.remove({'name':'hw'},{justOne:true}) //刪除一條數(shù)據(jù)
db.runoob.remove({'name':'hw'},{justOne:false}) //刪除全部數(shù)據(jù)
//查詢數(shù)據(jù)
$lt--小于 $lte--小于等于
$gt--大于 $gte--大于等于
$ne--不等于 $in $nin--是否處在該范圍
$and $or 查詢條件與或
$type
/^abc/ $regex:'abc$' 正則表達(dá)式
limit(num) 顯示指定數(shù)量的結(jié)果
skip(num) 跳過指定數(shù)量的結(jié)果
$where 查詢函數(shù)
_id默認(rèn)顯示,不顯示則把值設(shè)為0
sort() 排序,參數(shù)為1升序 -1 降序
count() 統(tǒng)計(jì)查詢結(jié)果數(shù)量 也可把查詢參數(shù)放進(jìn)count中
distinct() 消除重復(fù)數(shù)據(jù)
db.runoob.find({age:{$gte:18}})
db.runoob.find({age:{$in:[12,32,21]}})
db.runoob.find({$and:{age:{$in:[12,32,21]},{age:{$gte:18}}}})
db.runoob.find({age:{$gte:18}}).skip(3).limit(2)
db.runoob.find({age:/^abc/,name:{$regex:'123$'}})
db.runoob.find($where:function(){return this.age<=19})
db.runoob.find({age:{$gte:18}}).sort({age:1})
db.runoob.distinct({age:{$gte:18}})
//備份數(shù)據(jù)
mongodump -h dbhost -d dbname -o dbdirectory
-h 服務(wù)器地址
-d 需要備份的數(shù)據(jù)庫(kù)名稱
-o 備份數(shù)據(jù)庫(kù)存放位置
//數(shù)據(jù)恢復(fù)
mongorestore -h dbhost -d dbname --dir dbdirectory
-h 服務(wù)器地址
-d 需要恢復(fù)的數(shù)據(jù)庫(kù)實(shí)例
--dir 備份數(shù)據(jù)所在位置
//數(shù)據(jù)聚合
$group分組 $match過濾數(shù)據(jù) $project修改文檔結(jié)構(gòu)
$sort排序 $limit指定數(shù)量 $skip 跳過
$unwind 拆分?jǐn)?shù)組類型的字段 $pushAll
$sum 和 $avg 平均值 $push 添加值至數(shù)組
$pop $addToSet $pull $rename $bit
$first開頭 $last結(jié)尾 $min $max
db.runoob.aggregate({$group:{_id:'$name',count:{$sum:1},avg_age:{$avg:'$age'}}}
,{$project:{name:'$_id',count:'$count',avg_age:'$avg_age'}},
{$match:{age:{$gt:20}}},{$unwind:{'$age',preserveNullAndEmptyArrays:true}} //true保留缺失值
)
//建立唯一值的索引
db.runoob.ensureIndex({name:1},{'unique':true}) //1升序 -1降序
db.runoob.find({name:'he'}).explain('executionStats') //獲取時(shí)間
//查看集合中所有索引
db.runoob.getIndexes()
//刪除索引
db.runoob.dropIndex('name')
//監(jiān)控
Mongostat 檢測(cè)數(shù)據(jù)庫(kù)狀態(tài)
Mongotop sleeptime - -locks 跟蹤一個(gè)MongoDB的實(shí)例
總結(jié)
以上是生活随笔為你收集整理的insert自动跳过存在数据_轻松入门mongo 数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Leetcode】Python 代码本
- 下一篇: hibernate3连接mysql8报错