关于云开发数据库文档中,Aggregate下指定多个连接条件代码示例中有错误
下面这个示例中连接条件是orders的quantity字段大于等于books的stock字段,但是输出结果却是orders的quantity字段小于books的stock字段。
希望能更正一下。
指定多个连接条件
假设 orders
集合有以下记录:
[
{"_id":4,"book":"novel 1","price":300,"quantity":20},
{"_id":5,"book":"science 1","price":20,"quantity":1}
]
books
集合有以下记录:
[
{"_id":"book1","author":"author 1","category":"novel","stock":10,"time":1564456048486,"title":"novel 1"},
{"_id":"book3","author":"author 3","category":"science","stock":30,"title":"science 1"}
]
以下操作连接 orders
和 books
集合,要求两个条件:
orders
的book
字段与books
的title
字段相等orders
的quantity
字段大于或等于books
的stock
字段
const db = cloud.database()
const $ = db.command.aggregate
db.collection('orders').aggregate()
.lookup({
from: 'books',
let: {
order_book: '$book',
order_quantity: '$quantity'
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$title', '$$order_book']),
$.gte(['$stock', '$$order_quantity'])
])))
.project({
_id: 0,
title: 1,
author: 1,
stock: 1
})
.done(),
as: 'bookList',
})
.end()
.then(res => console.log(res))
.catch(err => console.error(err))
结果:
[
{
"_id": 4,
"book": "novel 1",
"price": 300,
"quantity": 20,
"bookList": []
},
{
"_id": 5,
"book": "science 1",
"price": 20,
"quantity": 1,
"bookList": [
{
"title": "science 1",
"author": "author 3",
"stock": 30
}
]
}
]