robo3T-操作MongoDB数据库常用命令
1、常用命令
1.1 新增字段【 NumberInt() 整型化;multi 為true時,選取全部數據(集合)】
????????命令:db.集合名.update({}, {'$set':{'新字段':字段值}}, {'multi': true})
1.2 指定刪除字段【exists 為true時,該字段存在】
????????命令:db.getCollection('集合名').update({'確定該字段是否存在':{$exists:true}}, {$unset:{'指定的字段':''}}, {multi:true})
1.3 查詢指定字段的文檔信息
????????命令:db.getCollection('集合名').find({'字段名':'字段屬性'})
1.4 查找某個字段不存在的文檔
????????命令:db.getCollection('集合名').find({'字段名':{$exists:false}})
1.5 查詢多個字段為過濾條件的文檔
? ? ? ? 命令:db.getCollection('集合名').find({'字段1':{$exists:false},'字段2':{$exists:true}})
1.6 查詢嵌套字段的文檔
????????例如:字段name是嵌套在people下的字段,即name是people的子字段。
????????查找所有name為“lucy”的文檔,則在people和name之間加點"."表示。
????????db.getCollection('集合名').find({'people.name':‘lucy’})
1.7 查詢大于、小于、等于字段的具體數值的文檔
????????db.getCollection('集合名').find({'字段名':{'$gt':字段值}})
????????$gt:大于; ? ?$lt:小于; ? ?$gte:大于或等于; ? ?$lte:小于或等于; $ne: 不等于
????????注:使用不等于時,"$ne"后面可以跟非數值型的數據,例如str類型。
????????例如 查詢字段name存在且不為空字符串:db.getCollection("集合名").find({"name":{"$exists":true, "$ne":""}})
1.8?刪除滿足某條件的文檔
????????命令:db.getCollection('集合名').remove({'字段名':'條件'})
1.9?update更新字段屬性值
? ? ? ? 命令:db.getCollection('集合名').update({'字段名':‘原數值’},{'$set':{'字段名':‘新數值’}},{multi:true})
1.10?按照指定排序輸出顯示
????????命令:db.getCollection('集合名').find().sort({"字段名":-1})?
1.11?使用正則匹配查詢某個字段中含有“某部分”內容的文檔(部分匹配):
? ? ? ? 命令:db.getCollection('集合名').find({post_text:{$regex:"runoob"}})
1.12 查詢文檔中單個字段信息的值
? ? ? ? 命令:db.getCollection('集合名').find({ name: {$exists: true}}, {name: 1})
總結
以上是生活随笔為你收集整理的robo3T-操作MongoDB数据库常用命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java开发知识总结1
- 下一篇: python怎么设置随机数种子_pyth