目录
基于$lookup命令,通过集合的关联查询,用户可以创建基于两个集合的视图并查询视图数据。应用在构建查询时,无需每次构建和维护复杂的管道查询。
db.inventory.insertMany([
{ prodId: 100, price: 20, quantity: 125},
{ prodId: 101, price: 10, quantity: 234},
{ prodId: 102, price: 15, quantity: 432},
{ prodId: 103, price: 17, quantity: 320}
])
db.orders.insertMany([
{ orderId: 201, custid: 301, prodId: 100, numPurchased: 20},
{ orderId: 202, custid: 302, prodId: 101, numPurchased: 10},
{ orderId: 203, custid: 303, prodId: 102, numPurchased: 5},
{ orderId: 204, custid: 303, prodId: 103, numPurchased: 15},
{ orderId: 205, custid: 303, prodId: 103, numPurchased: 20},
{ orderId: 206, custid: 302, prodId: 102, numPurchased: 1},
{ orderId: 207, custid: 302, prodId: 101, numPurchased: 5},
{ orderId: 208, custid: 301, prodId: 100, numPurchased: 10},
{ orderId: 209, custid: 303, prodId: 103, numPurchased: 30}
])
db.createView(
"v_sales", // 视图名称
"orders", // 基于orders集合创建视图
[{
$lookup: {// 通过lookup查询inventory集合
from: "inventory",
localField: "prodId",
foreignField: "prodId",
as: "inventoryDocs"
}
}, {
$project: { //指定输出字段
_id: 0,
prodId: 1,
orderId: 1,
numPurchased: 1,
price: "$inventoryDocs.price"
}
}, {
$unwind: "$price" //将price数组打平
}])
db.v_sales.find()
/* 1 */
{
"orderId" : 201,
"prodId" : 100,
"numPurchased" : 20,
"price" : 20
},
/* 2 */
{
"orderId" : 202,
"prodId" : 101,
"numPurchased" : 10,
"price" : 10
},
...
字符集中字符串的排列顺序,用于指定字符串的比较规则,包括大小写比较规则,发音标记规则顺序等。
Mongodb官网中,展示了一个带有默认字符序的视图
db.places.insertMany([
{ _id: 1, category: "café" },
{ _id: 2, category: "cafe" },
{ _id: 3, category: "cafE" }
])
db.createView(
"placesView",
"places",
[ { $project: {category: 1}}],
{collation: { locale: "fr", strength: 1}}
)
db.placesView.countDocuments({ category: "cafe"})
//结果返回3
//基于placesView创建新的视图,指定不同的字符序,创建失败。
db.createView(
"placesView2",
"placesView",
[ { $project: {category: 1}}],
{collation: { locale: "en", strength: 1}}
)
{
"message" : "View test.placesView2 has conflicting collation with view test.placesView",
"ok" : 0,
"code" : 167,
"codeName" : "OptionNotSupportedOnView"
}
//使用$lookup查询视图和不同字符序的集合,查询失败
db.places.aggregate()
.lookup({
from: "placesView",
localField: "category",
foreignField: "category",
as: "categoryViews"
})
{
"message" : "Cannot override a view's default collationtest.placesView",
"ok" : 0,
"code" : 167,
"codeName" : "OptionNotSupportedOnView"
}