如何使用 SomeTable.create 并在同一个查询中一起执行连接
How to use SomeTable.create and perform join together in the same query
我的问题是我的 mongoose Schema 中有一个像这样的静态方法
const chatMessageSchema = new Schema({
id: String,
something: String,
.
.
posted_by_user: String,
}, { collection: 'XXXX' });
并且在 chatMessageSchema 的静态方法之一中
chatMessageSchema.statics.createATextPostInChatRoom = function (roomid,
message) {
return this.create({
roomid: roomid,
message: message,
type: MESSAGE_TYPES.TYPE_TEXT,
notes: {},
posted_by_user: 'XXXXXXX',
read_by_recipients: posted_by_user
})
.then(post => post)
.catch(error => {
throw(error)
});
}
现在我想做的是
在创建过程中,我想在 posted_by_user: 'XXXX'
上执行连接,或者我是否必须在下一个 .then()
块中执行此连接
任何帮助将不胜感激。
我使用 MongoDB $lookup
方法做到了,我在 aggregation
.
中使用了这种方法
我的问题是我的 mongoose Schema 中有一个像这样的静态方法
const chatMessageSchema = new Schema({
id: String,
something: String,
.
.
posted_by_user: String,
}, { collection: 'XXXX' });
并且在 chatMessageSchema 的静态方法之一中
chatMessageSchema.statics.createATextPostInChatRoom = function (roomid,
message) {
return this.create({
roomid: roomid,
message: message,
type: MESSAGE_TYPES.TYPE_TEXT,
notes: {},
posted_by_user: 'XXXXXXX',
read_by_recipients: posted_by_user
})
.then(post => post)
.catch(error => {
throw(error)
});
}
现在我想做的是
在创建过程中,我想在 posted_by_user: 'XXXX'
上执行连接,或者我是否必须在下一个 .then()
块中执行此连接
任何帮助将不胜感激。
我使用 MongoDB $lookup
方法做到了,我在 aggregation
.