科技微讯

MongoDB 学习笔记

nodejs mongodb drivernodejs mongodb driver api

零散笔记

mongodb shell 常用命令

和 mongodb 的 nodejs driver 的 api 不完全相同。

const { result } = await db.collection("oppo").insertOne({ a: "b" });
await db.collection("user").insertOne({ id: 1, name: "tom" });
const doc = {
  id: 2,
  name: "jack",
  hobby: "swimming",
};
const { result: re } = await db
  .collection("user")
  .updateOne({ id: 2 }, { $setOnInsert: { ...doc } }, { upsert: true });

operator

update operator

db.collection.updateOne() 中的 operator 叫 update operator,update operator 可以分为 3 类,分别是 field operator、array update operator、bitwise update operator。

上面提到的 $inc$set$unset$setOnInsert 等都是 field operator,$push$pull$each$addToSet$pop$slice$sort 等都是 array update operator。bitwise operator 只有一个,就是 $bit

$sort 必须和 $each 一起用,如果只想 update 数组的 item 顺序,可以给 $each 提供一个空数组。

query operator

aggregation

The aggregation framework allows you to analyze your data in real time. Using the framework, you can create an aggregation pipeline that consists of one or more stages. Each stage transforms the documents and passes the output to the next stage.

aggregation 有三种模式,最常用的是 aggregation pipeline framework。

thumbsup0
thumbsdown0
暂无评论