Mongodb Week1

Posted by Borg on January 21, 2017

Mongodb University Courses Note

Mongodb University的M101P: MongoDB for Developers与M102: MongoDB for DBAs课程第一周笔记 官方文档地址

Start Mongod server

sudo service mongod start	# 使用默认设置
mongod --dbpath datadir		# 需要设置数据路径
mongo localhost/dbname
mongo

mongoimport

mongoimport --db dbname --collection collectionname < file.json/csv/tsv

insert

show dbs
use video
db.movies.insertOne({ "title": "Jaws", "year": 1975, "imdb": "tt0073195", "tags":["tag1","tag2"] });
db.movies.insertOne({ "title": "Mad Max 2: The Road Warrior", "year": 1981, "imdb": "tt0082694", "nested":{a:1} })
db.movies.insertOne({ "title": "Raiders of the Lost Ark", "year": 1981, "imdb": "tt0082971" })

Cursor

var c = db.movies.find({"title":"Jaws"})
c.hasNext()
c.next()

Query

find

db.movies.find({"title":"Jaws"},{"year":1, "_id":0}) # 第一个参数为请求,第二个参数设置请求的字段,相当于selectkey可以不用引号。。。
db.movies.find().limit(2).skip(1).toArray()
db.movies.find().pretty()
db.movies.findOne()

操作符: $gt, $gte, $lt, $lte, $or, $in, $nin, $not, $type, $exists

db.movies.find({year:{$gte:1980}})
db.movies.find({tags:{$exists:true}})
db.movies.find({tags:"tag1"}) # tags字段内包含"tag1"就可

内嵌文档查找 句点操作符必须用引号引起key

db.movies.find({"nested.a":1})

Update

j = db.movies.findOne()
j.title = "Test"
db.movies.save(j)

db.movies.updatemany({newkey:val})

sorting

db.movies.find({year:{$exists:true}}, {year:1,_id:0}).sort({year:1}) # 升序
db.movies.find({year:{$exists:true}}, {year:1,_id:0}).sort({year:-1}) # 降序
db.movies.find({year:{$exists:true}}, {year:1,_id:0}).sort({year:1, title:1}) # 多个字段排序