database,一个 database 中有多个 collection。
collections,类似 MySQL 中的 table。
同一个 database 中的所有 collections,会写入硬盘上的同一个文件。
插入操作
> use tutorial
switched to db tutorial
查询操作
> db.users.find({
... _id: ObjectId("5a7bfb5ac53b13638b843c78"),
... username: "waterdragon",
... })
{ "_id" : ObjectId("5a7bfb5ac53b13638b843c78"), "username" : "waterdragon" }
等价于
> db.users.find({ $and: [
... { _id: ObjectId("5a7bfb5ac53b13638b843c78") },
... { username: "waterdragon" }
... ] })
{ "_id" : ObjectId("5a7bfb5ac53b13638b843c78"), "username" : "waterdragon" }
更新操作
删除操作
索引和explain()
返回了 4 条记录(nReturned),搜索了 20000 条记录(totalDocsExamined)。
一个索引都没用上(totalKeysExamined)。
Basic Administration
> show databases
local 0.000GB
tutorial 0.001GB
> use tutorial
> show collections
numbers
> db.stats()
{
"db" : "tutorial",
"collections" : 1,
"objects" : 20000,
"avgObjSize" : 35,
"dataSize" : 700000,
"storageSize" : 352256,
"numExtents" : 0,
"indexes" : 2,
"indexSize" : 389120,
"ok" : 1
}
...
看函数的代码实现
function (obj, opts) {
if (obj == null)
throw "can't save a null";
if (typeof(obj) == "number" || typeof(obj) == "string")
throw "can't save a number or string"
if (typeof(obj._id) == "undefined") {
obj._id = new ObjectId();
return this.insert(obj, opts);
}
else {
return this.update(, obj, Object.merge(, opts));
}
}
领取专属 10元无门槛券
私享最新 技术干货