MongoDB创建和查询视图(二)

目录

基于$lookup创建关联查询视图

创建带有默认字符序的视图

字符序

创建视图

视图中的字符序


基于$lookup创建关联查询视图

基于$lookup命令,通过集合的关联查询,用户可以创建基于两个集合的视图并查询视图数据。应用在构建查询时,无需每次构建和维护复杂的管道查询。

  • 构建用于创建视图的两个集合, inventory集合和orders集合
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官网中,展示了一个带有默认字符序的视图

  • 创建一个不同cafe值的集合
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

视图中的字符序

  • 用户可以在创建视图时,指定字符序。如果没有指定字符序,视图默认的字符序就是简单的二进制字符序。视图并不会继承集合的默认字符序。
  • 视图中字符比较,使用默认的字符序。禁止尝试修改或覆盖字符序的操作
  • 基于已有view创建的视图,不可以指定与原始图不同的字符序
  • 在多个view中进行aggregation查询时,如$lookup, $graphLookup命令,使用的视图必须有相同的字符序。
//基于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"
}

猜你喜欢

转载自blog.csdn.net/wilsonzane/article/details/135204642