共计 4906 个字符,预计需要花费 13 分钟才能阅读完成。
模拟数据准备
下载测试数据
# 导入模拟数据
[root@iZwz9jcsi5zvkh0ck4vv67Z ~]# git clone https://github.com/huynhsamha/quick-mongo-atlas-datasets.git
[root@iZwz9jcsi5zvkh0ck4vv67Z ~]# cd quick-mongo-atlas-datasets-master
[root@iZwz9jcsi5zvkh0ck4vv67Z quick-mongo-atlas-datasets-master]# tree
.
├── dump
│ ├── sample_airbnb
│ │ ├── listingsAndReviews.bson
│ │ └── listingsAndReviews.metadata.json
│ ├── sample_geospatial
│ │ ├── shipwrecks.bson
│ │ └── shipwrecks.metadata.json
│ ├── sample_mflix
│ │ ├── comments.bson
│ │ ├── comments.metadata.json
│ │ ├── movies.bson
│ │ ├── movies.metadata.json
│ │ ├── sessions.bson
│ │ ├── sessions.metadata.json
│ │ ├── theaters.bson
│ │ ├── theaters.metadata.json
│ │ ├── users.bson
│ │ └── users.metadata.json
│ ├── sample_supplies
│ │ ├── sales.bson
│ │ └── sales.metadata.json
│ ├── sample_training
│ │ ├── companies.bson
│ │ ├── companies.metadata.json
│ │ ├── grades.bson
│ │ ├── grades.metadata.json
│ │ ├── inspections.bson
│ │ ├── inspections.metadata.json
│ │ ├── posts.bson
│ │ ├── posts.metadata.json
│ │ ├── routes.bson
│ │ ├── routes.metadata.json
│ │ ├── stories.bson
│ │ ├── stories.metadata.json
│ │ ├── trips.bson
│ │ ├── trips.metadata.json
│ │ ├── tweets.bson
│ │ ├── tweets.metadata.json
│ │ ├── zips.bson
│ │ └── zips.metadata.json
│ └── sample_weatherdata
│ ├── data.bson
│ └── data.metadata.json
└── README.md
7 directories, 37 files
导入测试数据
[root@iZwz9jcsi5zvkh0ck4vv67Z ~]# cd quick-mongo-atlas-datasets-master
# 请自行修改自己为自己的用户密码
[root@iZwz9jcsi5zvkh0ck4vv67Z quick-mongo-atlas-datasets-master]# mongorestore dump/ --uri=mongodb://restoreuser:123456@10.200.1.98:27017
查看数据
数据库操作
创建数据库
创建集合
增加数据
查询数据
# 选择使用sample_mflix库
use sample_mflix
# 查询集合
show collections
# 查询movies集合所有数据
db.movies.find()
db.movies.find().count()
查询find()函数语法
{ <field1>: { <operator1>: <value1> }, ... }
# db.collection.find(query, projection)
# query :可选,使用查询操作符指定查询条件
# projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。
常用比较语法
- $gt:大于
- $eq:等于
- $gte:大于或等于
- $in:包含
- $lt:小于
- $lte:小于或等于
- $ne:不等于
- $nin:不包含
db.movies.find({year: 1893})
{ _id: ObjectId("573a1390f29313caabcd4135"),
plot: 'Three men hammer on an anvil and pass a bottle of beer around.',
genres: [ 'Short' ],
runtime: 1,
cast: [ 'Charles Kayser', 'John Ott' ],
num_mflix_comments: 1,
title: 'Blacksmith Scene',
fullplot: 'A stationary camera looks at a large anvil with a blacksmith behind it and one on either side. The smith in the middle draws a heated metal rod from the fire, places it on the anvil, and all three begin a rhythmic hammering. After several blows, the metal goes back in the fire. One smith pulls out a bottle of beer, and they each take a swig. Then, out comes the glowing metal and the hammering resumes.',
countries: [ 'USA' ],
released: 1893-05-09T00:00:00.000Z,
directors: [ 'William K.L. Dickson' ],
rated: 'UNRATED',
awards: { wins: 1, nominations: 0, text: '1 win.' },
lastupdated: '2015-08-26 00:03:50.133000000',
year: 1893,
imdb: { rating: 6.2, votes: 1189, id: 5 },
type: 'movie',
tomatoes:
{ viewer: { rating: 3, numReviews: 184, meter: 32 },
lastUpdated: 2015-06-28T18:34:09.000Z } }
db.movies.find({year: {$eq:1893}})
db.movies.find({year: {$gt:1893}}).count()
db.movies.find({year: {$gte:1893}}).count()
db.movies.find({year: {$lt:1893}}).count()
db.movies.find({year: {$lte:1893}}).count()
db.movies.find({year: {$ne:1893}}).count()
db.movies.find({year: {$in:[1893,1896]}}).count()
db.movies.find({year: {$nin:[1893,,1894,1895,1896]}}).count()
逻辑查询
- $and:逻辑与
- $not:反转
- $nor:逻辑非
- $or:逻辑或
db.movies.find( { countries: "Mexico", "imdb.rating": { $gte: 7 } } )
db.movies.find( {$and:[ {countries: "Mexico"}, {"imdb.rating": { $gte: 7 }} ]} ).count()
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
db.inventory.find( { item: { $not: /^p.*/ } } )
db.inventory.find( { item: { $not: { $regex: "^p.*" } } } )
db.inventory.find( { item: { $not: { $regex: /^p.*/ } } } )
db.inventory.find( { $nor: [ { price: 1.99 }, { sale: true } ] } )
db.inventory.find( { $nor: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )
db.inventory.find( { $nor: [ { price: 1.99 }, { price: { $exists: false } },
{ sale: true }, { sale: { $exists: false } } ] } )
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
db.inventory.createIndex( { quantity: 1 } )
db.inventory.createIndex( { price: 1 } )
其他特殊查询
- $expr
- $jsonSchema
- $mod
- $regex
- $text
- $where
db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } )
db.inventory.find( { qty: { $mod: [ 4, 0 ] } } )
db.inventory.find( { item: { $not: { $regex: "^p.*" } } } )
db.inventory.find( { item: { $not: { $regex: /^p.*/ } } } )
db.products.find( { description: { $regex: /S/ } } )
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
db.articles.find( { $text: { $search: "coffee" } } )
db.articles.find( { $text: { $search: "\"coffee shop\"" } } )
db.articles.find( { $text: { $search: "bake coffee cake" } } )
db.players.insertMany([
{ _id: 12378, name: "Steve", username: "steveisawesome", first_login: "2017-01-01" },
{ _id: 2, name: "Anya", username: "anya", first_login: "2001-02-02" }
])
db.players.find( { $where: function() {
return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e994")
} } );
db.players.find( {$expr: { $function: {
body: function(name) { return hex_md5(name) == "9b53e667f30cd329dca1ec9e6a83e994"; },
args: [ "$name" ],
lang: "js"
} } } )
删除数据
更新数据
修改数据
正文完