欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報激活碼778899分享:數(shù)據(jù)庫 Mongodb命令大全

柚子快報激活碼778899分享:數(shù)據(jù)庫 Mongodb命令大全

http://yzkb.51969.com/

目錄

數(shù)據(jù)庫命令

集合(表)命令

文檔(數(shù)據(jù))命令

_id 字段說明

插入

插入單個文檔(插入一條數(shù)據(jù))?insertOne

插入多個文檔(插入多條數(shù)據(jù))?insertMany

修改

更新一條文檔(修改一條數(shù)據(jù))?updateOne

更新多個文檔(修改多條數(shù)據(jù))?updateMany

替換文檔中的所有字段值??replaceOne

重命名字段??$rename

向文檔中添加時間字段,時間為當前時間??$currentDate

向文檔中的數(shù)組插入數(shù)據(jù)?$addToSet

從文檔數(shù)組中移除數(shù)據(jù)??$pull

刪除

刪除一個文檔(刪除一條數(shù)據(jù))?deleteOne

刪除多個文檔(刪除多條數(shù)據(jù))?deleteMany

刪除集合,包括集合中的所有文檔(刪除表)

刪除集合中的所有文檔,保留集合

批量操作?bulkWrite

查詢

查詢單個文檔(查詢單個數(shù)據(jù))?findOne

查詢多個文檔(查詢多個數(shù)據(jù))?find

范圍查詢

and 查詢

or 查詢

not 查詢

nor 查詢

in 查詢

nin 查詢

正則查詢?regex

內(nèi)嵌文檔查詢

數(shù)組文檔查詢

去重

查詢排序?sort

分頁查詢??skip? ?limit

查詢只返回指定列 投影

查詢不返回指定的列 投影

查詢統(tǒng)計個數(shù)?count

聚合查詢

1. 查詢用戶表中name=tom的文檔

2. 查詢訂單表中 amount大于 300 的訂單

3. 查詢訂單中 status = 1 并且 amount 大于 300 的訂單

4. 內(nèi)嵌查詢

5. 查找 amount?大于 200 的訂單,并按 amount?降序排序

分組查詢 $group

1. 按 item 字段分組并計算總銷售量

2.?按 date 字段分組并計算總銷售額

3.?按 item 分組,并計算每種商品的平均價格和總銷售量

4.?按 item 分組,并創(chuàng)建一個包含所有銷售日期的數(shù)組

5.?按 item 分組,并創(chuàng)建一個包含唯一銷售日期的數(shù)組

計劃查詢?$project

1. 查詢結(jié)果只返回指定字段 投影

2. 查詢結(jié)果不返回指定字段 投影

3. 重命名字段

4.?計算新字段

5. 只顯示指定的嵌套字段

內(nèi)聯(lián)查詢?$lookup

1. 兩張集合,關聯(lián)查詢

2. 三張集合,關聯(lián)查詢

外聯(lián)查詢?$unwind

數(shù)據(jù)庫命令

選擇數(shù)據(jù)庫

// 語法

use

// 示例

use test

顯示當前所在庫

db

顯示數(shù)據(jù)庫列表

show dbs

刪除當前所在數(shù)據(jù)庫

db.dropDatabase()

創(chuàng)建數(shù)據(jù)庫

在插入數(shù)據(jù)時自動創(chuàng)建

集合(表)命令

顯示集合列表

show collections

創(chuàng)建集合

// 語法

db.createCollection(, { options })

// 示例

db.createCollection("products")

// 創(chuàng)建固定大小的集合,當集合達到指定的大小時,它會覆蓋最早的文檔。固定大小的集合的文檔一旦插入就不允許修改

// 示例,創(chuàng)建固定大小的集合,capped=true表示集合有大小上限,size是字節(jié)單位

db.createCollection("products", {capped: true, size: 1048576 })

// 示例,創(chuàng)建固定大小的集合,capped=true表示集合有大小上限,size是字節(jié)單位,max是最大文檔數(shù)

db.createCollection("products", {capped: true, size: 1048576, max: 1000})

刪除集合

// 語法

db..drop()

// 示例

db.products.drop()

文檔(數(shù)據(jù))命令

_id 字段說明

_id 字段是每個文檔的唯一標識符,確保每個文檔在集合中是唯一的。這個字段在每個文檔中都是必須的,并且在插入文檔時會自動生成。如果沒有顯式指定 _id,MongoDB 會為其生成一個唯一的 ObjectId。

假如我們在插入數(shù)據(jù)時沒有指定_id值

db.users.insertOne({ "name": "Alice", "age": 25 })

MongoDB 會自動生成一個 ObjectId 作為 _id,如下:

{

"_id": ObjectId("60a7c0c8b59b3c001c8e4f2d"),

"name": "Alice",

"age": 25

}

使用 _id 進行查詢

db.users.find({ "_id": ObjectId("60a7c0c8b59b3c001c8e4f2d") })

指定_id值

db.users.insertOne({ "_id": 1, "name": "Bob", "age": 30 })

{

"_id": 1,

"name": "Bob",

"age": 30

}

使用 _id 進行查詢

db.users.find({ "_id": 1 })

插入

插入單個文檔(插入一條數(shù)據(jù))?insertOne

// 語法

db..insertOne({})

// 示例

db.users.insertOne({

name: "Tom",

age: 30,

email: "tom@example.com"

})

插入多個文檔(插入多條數(shù)據(jù))?insertMany

// 語法

db..insertMany([

{}, {}

])

// 示例

db.orders.insertMany([

{ item: "book", qty: 10, price: 15 },

{ item: "pen", qty: 20, price: 1.5 }

])

修改

更新一條文檔(修改一條數(shù)據(jù))?updateOne

1.?更新 name 為 "Alice" 的文檔的 age 字段

db.users.updateOne(

{ name: "Alice" },

{ $set: { age: 26 } }

)

2.?如果沒有找到文檔,則插入一個新文檔? upsert: true

db.users.updateOne(

{ name: "Charlie" },

{ $set: { age: 35 }, $setOnInsert: { createdAt: new Date() } },

{ upsert: true }

)

更新多個文檔(修改多條數(shù)據(jù))?updateMany

1. 將所有 age 小于 30 的用戶的 status 更新為 "young"

db.users.updateMany(

{ age: { $lt: 30 } },

{ $set: { status: "young" } }

)

2.?如果沒有找到文檔,則插入一個新文檔??upsert: true?

db.users.updateMany(

{ status: "inactive" },

{ $set: { status: "active" } },

{ upsert: true }

)

替換文檔中的所有字段值??replaceOne

1. 用新文檔替換 name 為 "Alice" 的文檔

db.users.replaceOne(

{ name: "Alice" },

{ name: "Alice", age: 27, status: "active" }

)

2.?如果沒有找到文檔,則插入一個新文檔??upsert: true

db.users.replaceOne(

{ name: "David" },

{ name: "David", age: 40, status: "new" },

{ upsert: true }

)

重命名字段??$rename

db.users.updateOne(

{ name: "Alice" },

{ $rename: { "address.city": "address.town" } }

)

向文檔中添加時間字段,時間為當前時間??$currentDate

db.users.updateOne(

{ name: "Alice" },

{ $currentDate: { create_time: true } }

)

向文檔中的數(shù)組插入數(shù)據(jù)?$addToSet

db.users.updateOne(

{ name: "Tom" },

{ $addToSet: { address: "Shenzhen" } }

)

從文檔數(shù)組中移除數(shù)據(jù)??$pull

db.users.updateOne(

{ name: "Tom" },

{ $pull: { address: "Shenzhen" } }

)

刪除

刪除一個文檔(刪除一條數(shù)據(jù))?deleteOne

db.users.deleteOne({ name: "Tom" })

刪除多個文檔(刪除多條數(shù)據(jù))?deleteMany

// 刪除所有 age 小于 30 的文檔

db.users.deleteMany({ age: { $lt: 30 } })

刪除集合,包括集合中的所有文檔(刪除表)

// 語法

db..drop()

// 示例

db.users.drop()

刪除集合中的所有文檔,保留集合

db.users.deleteMany({})

批量操作?bulkWrite

bulkWrite() 方法允許執(zhí)行多種插入、更新和刪除操作的批量操作。它適用于需要一次性處理多個寫操作的場景。

執(zhí)行多種操作(插入、更新、刪除)

db.users.bulkWrite([

{ insertOne: { document: { name: "Grace", age: 29, email: "grace@example.com" } } },

{ updateOne: { filter: { name: "Bob" }, update: { $set: { age: 31 } } } },

{ deleteOne: { filter: { name: "Charlie" } } }

])

設置 ordered 選項為 false,使得即使遇到錯誤也會繼續(xù)執(zhí)行其他操作

db.users.bulkWrite([

{ insertOne: { document: { name: "Hank", age: 45, email: "hank@example.com" } } },

{ updateOne: { filter: { name: "David" }, update: { $set: { status: "active" } } } }

], { ordered: false })

查詢

查詢操作符

符號說明$eq等于$ne不等于$gt大于$lt小于$gte大于等于$lte小于等于

邏輯操作符

符號說明$and邏輯與$or邏輯或$not邏輯非$nor邏輯非或

查詢單個文檔(查詢單個數(shù)據(jù))?findOne

// 語法

db..findOne({ 列名: "值" });

// 示例,對比mysql:select * from users where name = 'Tom'

db.users.findOne({ name: "Tom" });

查詢多個文檔(查詢多個數(shù)據(jù))?find

// 示例,對比mysql:select * from users where age > 25

db.users.find({ age: { $gt: 25 } });

// 將返回結(jié)果游標轉(zhuǎn)成數(shù)組

db.users.find({ age: { $gt: 25 } }).toArray();

范圍查詢

// 查找年齡在 18 到 30 歲之間的用戶

db.users.find({ age: { $gte: 18, $lte: 30 } });

and 查詢

// 查找年齡大于 25 歲且性別為 "男" 的用戶

db.users.find({ $and: [{ age: { $gt: 25 } }, { gender: "男" }] });

or 查詢

// 查找年齡小于 18 歲或大于 60 歲的用戶

db.users.find({ $or: [{ age: { $lt: 18 } }, { age: { $gt: 60 } }] });

not 查詢

// 查詢 age 不等于 25 的文檔

db.users.find({

age: { $not: { $eq: 25 } }

});

// 等價于

db.users.find({

age: { $ne: 25 }

});

nor 查詢

nor可以理解為多個not條件的組合

// 查詢年齡不是 25 且地址不是Shenzhen的文檔

db.users.find({

$nor: [

{ age: 25 },

{ address: "Shenzhen" }

]

});

// 查詢年齡不是 25 或 30 的用戶, 且地址不是Shenzhen的文檔

db.users.find({

$nor: [

{ age: 25 },

{ age: 30},

{ address: "Shenzhen" }

]

});

in 查詢

// 查詢年齡是 25 和 35 的文檔

db.users.find({ age: { $in: [25, 35] } });

nin 查詢

// 查詢年齡不是是 25 和 35 的文檔

db.users.find({ age: { $nin: [25, 35] } });

正則查詢?regex

// 查詢 name 中包含 Tom 的文檔

db.users.find({

name: { $regex: "Tom" }

});

// i 忽略大小寫

db.users.find({

name: { $regex: "tom", $options: "i" }

});

// 匹配電子郵件以 "dave" 開頭的用戶

db.users.find({

email: { $regex: "^dave" }

});

// 匹配電子郵件以 "domain.com" 結(jié)尾的用戶

db.users.find({

email: { $regex: "domain\\.com$" }

});

// 多行模式,匹配電子郵件包含 "example" 或 "domain" 的用戶

db.users.find({

email: { $regex: "example|domain" }

});

內(nèi)嵌文檔查詢

// 數(shù)據(jù)結(jié)構

[

{

"_id": 1,

"name": "Alice",

"age": 30,

"address": {

"street": "123 Elm St",

"city": "New York",

"state": "NY",

"zip": "10001"

}

},

{

"_id": 2,

"name": "Bob",

"age": 25,

"address": {

"street": "456 Oak St",

"city": "Los Angeles",

"state": "CA",

"zip": "90001"

}

}

]

// 查詢語句,查找有地址的用戶,并且地址的城市是 "New York"

db.users.find({ "address.city": "New York" });

數(shù)組文檔查詢

$elemMatch:匹配數(shù)組中滿足條件的元素

// 數(shù)據(jù)結(jié)構

[

{

"_id": 1,

"name": "Alice",

"age": 30,

"friends": [

{ "name": "Bob", "age": 25 },

{ "name": "Charlie", "age": 32 }

]

},

{

"_id": 2,

"name": "David",

"age": 28,

"friends": [

{ "name": "Eve", "age": 22 },

{ "name": "Frank", "age": 29 }

]

}

]

// 查詢語句,查找friends中包含年齡大于 30 歲的用戶

db.users.find({ "friends": { $elemMatch: { age: { $gt: 30 } } } })

去重

// 給 age 字段去重

db.users.distinct("age");

// age 大于 25 的用戶的去重 name 字段

db.users.distinct("name", { age: { $gt: 25 } });

查詢排序?sort

// 使用關鍵字sort,按照 age 升序排序

db.users.find().sort({ age: 1 });

// 按照 age 降序排序

db.users.find().sort({ age: -1 });

分頁查詢??skip? ?limit

// 使用 limit 關鍵字,返回前5條數(shù)據(jù)

db.users.find().limit(5);

// 分頁查詢,跳過前 5 個文檔,并返回接下來的 5 個文檔

db.users.find().skip(5).limit(5)

查詢只返回指定列 投影

// 返回 name age,不返回_id, 0表示不返回,1表示返回

db.users.find({}, { name: 1, age: 1, _id: 0 });

查詢不返回指定的列 投影

// 不返回 name, 其他字段都返回,0表示不返回,1表示返回

db.users.find({}, { name: 0 });

查詢統(tǒng)計個數(shù)?count

// 查詢集合中的所有文檔數(shù)量

db.users.count();

// 查詢年齡大于 30 的文檔的個數(shù)

db.users.count({ age: { $gt: 10 } });

db.users.find({ age: { $gt: 10 } }).count()

// 這里要使用聚合aggregate,$match的意思和find一樣

db..aggregate([

{ $match: { name: "Tom" } },

{ $count: "total" }

])

聚合查詢

聚合操作符

符號說明$sum計算和$avg計算平均值$min計算最小值$max計算最大值$first取每組中的第一個文檔$last取每組中的最后一個文檔$push將每組中的某個值添加到數(shù)組中$addToSet將每組中的唯一值添加到數(shù)組中(去重)$count計算文檔數(shù)量

算數(shù)操作符

符號說明$add兩個數(shù)、日期、時間相加$subtract兩個數(shù)、日期、時間相減$multiply兩個數(shù)相乘$divide兩個數(shù)相除$mod兩個數(shù)取余$abs數(shù)字的絕對值$ceil數(shù)字向上取整$floor數(shù)字向下取整$exp返回 e(自然對數(shù)的底數(shù))取指定數(shù)字次方的值$ln數(shù)字的自然對數(shù)$log數(shù)字以指定底數(shù)為底的對數(shù)$log10數(shù)字以10為底的對數(shù)$pow指定數(shù)字的指定次方的值$sqrt數(shù)字的平方根$trunc數(shù)字截斷為整數(shù)的值(刪除小數(shù)部分)

$match

$match 是 MongoDB 聚合管道中的一個重要階段,用于篩選文檔,僅輸出符合指定條件的文檔。它的作用類似于 find 操作,但它是聚合管道的一部分,通常用于數(shù)據(jù)預處理和過濾,以便后續(xù)階段能夠處理更少、更相關的數(shù)據(jù)。

語法:

db..aggregate([

{ $match: { } }

])

// :標準的查詢條件,類似于 find 方法中的查詢條件。

1. 查詢用戶表中name=tom的文檔

db.users.aggregate([

{ $match: { name: "Tom" } }

])

2. 查詢訂單表中 amount大于 300 的訂單

db.orders.aggregate([

{ $match: { amount: { $gt: 300 } } }

])

3. 查詢訂單中 status = 1 并且 amount 大于 300 的訂單

db.orders.aggregate([

{ $match: { $and: [ { status: "A" }, { amount: { $gt: 300 } } ] } }

])

4. 內(nèi)嵌查詢

db.orders.aggregate([

{ $match: { "address.city": "New York" } }

])

5. 查找 amount?大于 200 的訂單,并按 amount?降序排序

db.orders.aggregate([

{ $match: { amount: { $gt: 200 } } },

{ $sort: { amount: -1 } }

])

分組查詢 $group

有sales集合數(shù)據(jù)如下

[

{ "_id": 1, "item": "apple", "price": 10, "quantity": 2, "date": "2023-01-01" },

{ "_id": 2, "item": "banana", "price": 5, "quantity": 10, "date": "2023-01-01" },

{ "_id": 3, "item": "apple", "price": 10, "quantity": 5, "date": "2023-01-02" },

{ "_id": 4, "item": "banana", "price": 5, "quantity": 7, "date": "2023-01-02" },

{ "_id": 5, "item": "orange", "price": 8, "quantity": 8, "date": "2023-01-03" }

]

1. 按 item 字段分組并計算總銷售量

db.sales.aggregate([

{

$group: {

_id: "$item",

totalQuantity: { $sum: "$quantity" }

}

}

])

結(jié)果

[

{ "_id": "apple", "totalQuantity": 7 },

{ "_id": "banana", "totalQuantity": 17 },

{ "_id": "orange", "totalQuantity": 8 }

]

2.?按 date 字段分組并計算總銷售額

db.sales.aggregate([

{

$group: {

_id: "$date",

totalSales: { $sum: { $multiply: ["$price", "$quantity"] } }

}

}

])

結(jié)果

[

{ "_id": "2023-01-01", "totalSales": 70 },

{ "_id": "2023-01-02", "totalSales": 85 },

{ "_id": "2023-01-03", "totalSales": 64 }

]

3.?按 item 分組,并計算每種商品的平均價格和總銷售量

db.sales.aggregate([

{

$group: {

_id: "$item",

avgPrice: { $avg: "$price" },

totalQuantity: { $sum: "$quantity" }

}

}

])

結(jié)果

[

{ "_id": "apple", "avgPrice": 10, "totalQuantity": 7 },

{ "_id": "banana", "avgPrice": 5, "totalQuantity": 17 },

{ "_id": "orange", "avgPrice": 8, "totalQuantity": 8 }

]

4.?按 item 分組,并創(chuàng)建一個包含所有銷售日期的數(shù)組

db.sales.aggregate([

{

$group: {

_id: "$item",

dates: { $push: "$date" }

}

}

])

結(jié)果

[

{ "_id": "apple", "dates": ["2023-01-01", "2023-01-02"] },

{ "_id": "banana", "dates": ["2023-01-01", "2023-01-02"] },

{ "_id": "orange", "dates": ["2023-01-03"] }

]

5.?按 item 分組,并創(chuàng)建一個包含唯一銷售日期的數(shù)組

db.sales.aggregate([

{

$group: {

_id: "$item",

uniqueDates: { $addToSet: "$date" }

}

}

])

結(jié)果

[

{ "_id": "apple", "uniqueDates": ["2023-01-01", "2023-01-02"] },

{ "_id": "banana", "uniqueDates": ["2023-01-01", "2023-01-02"] },

{ "_id": "orange", "uniqueDates": ["2023-01-03"] }

]

6. 查詢結(jié)果后再分組

db.sales.aggregate([

{ $match: { item: "apple" } },

{

$group: {

_id: "$date",

totalQuantity: { $sum: "$quantity" },

avgPrice: { $avg: "$price" }

}

}

])

結(jié)果

[

{ "_id": "2023-01-01", "totalQuantity": 2, "avgPrice": 10 },

{ "_id": "2023-01-02", "totalQuantity": 5, "avgPrice": 10 }

]

7. 對分組結(jié)果進行排序

db.sales.aggregate([

{

$group: {

_id: "$item",

totalQuantity: { $sum: "$quantity" }

}

},

{ $sort: { totalQuantity: -1 } }

])

結(jié)果

[

{ "_id": "banana", "totalQuantity": 17 },

{ "_id": "apple", "totalQuantity": 7 },

{ "_id": "orange", "totalQuantity": 8 }

]

計劃查詢?$project

有users集合數(shù)據(jù)如下

[

{ "_id": 1, "name": "Alice", "age": 25, "address": { "city": "New York", "state": "NY" }, "score": 80 },

{ "_id": 2, "name": "Bob", "age": 30, "address": { "city": "Los Angeles", "state": "CA" }, "score": 90 }

]

1. 查詢結(jié)果只返回指定字段 投影

db.users.aggregate([

{

$project: {

name: 1,

age: 1

}

}

])

結(jié)果

[

{ "_id": 1, "name": "Alice", "age": 25 },

{ "_id": 2, "name": "Bob", "age": 30 }

]

2. 查詢結(jié)果不返回指定字段 投影

db.users.aggregate([

{

$project: {

address: 0

}

}

])

結(jié)果

[

{ "_id": 1, "name": "Alice", "age": 25, "score": 80 },

{ "_id": 2, "name": "Bob", "age": 30, "score": 90 }

]

3. 重命名字段

db.users.aggregate([

{

$project: {

username: "$name",

age: 1

}

}

])

結(jié)果

[

{ "_id": 1, "username": "Alice", "age": 25 },

{ "_id": 2, "username": "Bob", "age": 30 }

]

4.?計算新字段

計算新的字段 discountedScore,其值為 score 的 90%:

db.users.aggregate([

{

$project: {

name: 1,

age: 1,

discountedScore: { $multiply: ["$score", 0.9] }

}

}

])

結(jié)果

[

{ "_id": 1, "name": "Alice", "age": 25, "discountedScore": 72 },

{ "_id": 2, "name": "Bob", "age": 30, "discountedScore": 81 }

]

5. 只顯示指定的嵌套字段

db.users.aggregate([

{

$project: {

name: 1,

age: 1,

"address.city": 1

}

}

])

結(jié)果

[

{ "_id": 1, "name": "Alice", "age": 25, "address": { "city": "New York" } },

{ "_id": 2, "name": "Bob", "age": 30, "address": { "city": "Los Angeles" } }

]

內(nèi)聯(lián)查詢?$lookup

$lookup 是 MongoDB 的一個聚合管道操作符,用于執(zhí)行左外連接(left outer join)。它允許你將來自不同集合的數(shù)據(jù)合并到一個文檔中。$lookup 操作符將當前集合中的每個文檔與另一個集合中的文檔進行匹配,并將匹配的結(jié)果添加到當前文檔中。(類似于 SQL 的 left join)

1. 兩張集合,關聯(lián)查詢

有兩個集合?orders 訂單表和?products 產(chǎn)品表,orders 集合中的文檔中的?productId 字段,指向 products 集合中 _id 字段,數(shù)據(jù)內(nèi)容如下:

// orders 集合

{ "_id": 1, "productId": 101, "quantity": 2 }

{ "_id": 2, "productId": 102, "quantity": 1 }

// products 集合

{ "_id": 101, "name": "Widget", "price": 19.99 }

{ "_id": 102, "name": "Gadget", "price": 29.99 }

內(nèi)聯(lián)查詢語句

db.orders.aggregate([

{

$lookup: {

from: "products",

localField: "productId",

foreignField: "_id",

as: "productDetails"

}

}

])

參數(shù)說明:

db.orders.aggregate([...]):aggregate 方法在 orders 集合上運行。這意味著整個聚合管道操作是針對 orders 集合的。

from:指定要連接的目標集合的名稱。示例中我們是從?products 集合中查詢關聯(lián)數(shù)據(jù)。

localField:指定當前集合(orders源集合)中用于連接的字段名稱。示例中和products關聯(lián)的是productId字段。

foreignField:指定目標集合(from 集合)中用于連接的字段名稱,示例中和orders關聯(lián)的是_id字段。

as:查詢結(jié)果的別名,指定輸出數(shù)組字段的名稱。

2. 三張集合,關聯(lián)查詢

orders 訂單表和?products 表關聯(lián),products 表和 categories 關聯(lián),數(shù)據(jù)內(nèi)容如下:

// orders 訂單表

{ "_id": 1, "productId": 101, "quantity": 2 }

{ "_id": 2, "productId": 102, "quantity": 1 }

// products 產(chǎn)品表

{ "_id": 101, "categoryId": 201, "name": "Widget" }

{ "_id": 102, "categoryId": 202, "name": "Gadget" }

// categories 產(chǎn)品類別表

{ "_id": 201, "name": "Electronics" }

{ "_id": 202, "name": "Home Goods" }

內(nèi)聯(lián)查詢語句

db.orders.aggregate([

{

$lookup: {

from: "products",

localField: "productId",

foreignField: "_id",

as: "productDetails"

}

},

{

$lookup: {

from: "categories",

localField: "productDetails.categoryId",

foreignField: "_id",

as: "productDetails.categoryDetails"

}

}

])

外聯(lián)查詢?$unwind

$unwind 是 MongoDB 聚合管道中的另一個操作符,用于將數(shù)組字段展開成多個文檔。如果在使用 $lookup 時,目標集合的字段是一個數(shù)組(如上面的例子中 productDetails 是一個數(shù)組),使用 $unwind 可以將每個數(shù)組元素展開為獨立的文檔。

示例一:配置 $lookup 使用

db.orders.aggregate([

{

$lookup: {

from: "products",

localField: "productId",

foreignField: "_id",

as: "productDetails"

}

},

{

$unwind: "$productDetails"

}

])

示例二:展開數(shù)組

有如下基礎數(shù)據(jù):

db.users.insertMany([

{ _id: 1, name: "Alice", address: ["廣州", "北京", "杭州"] },

{ _id: 2, name: "Bob", address: ["上海", "武漢"] },

{ _id: 3, name: "Charlie", address: [] }

])

外聯(lián)查詢語句

db.users.aggregate([

{ $unwind: "$address" }

])

查詢結(jié)果

[

{ "_id": 1, "name": "Alice", "address": "廣州" },

{ "_id": 1, "name": "Alice", "address": "北京" },

{ "_id": 1, "name": "Alice", "address": "杭州" },

{ "_id": 2, "name": "Bob", "address": "上海" },

{ "_id": 2, "name": "Bob", "address": "武漢" }

]

柚子快報激活碼778899分享:數(shù)據(jù)庫 Mongodb命令大全

http://yzkb.51969.com/

推薦閱讀

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權,聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19357829.html

發(fā)布評論

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄