Mongodb 由子文档ID填充的mongoose
假设以下模式: AschemaMongodb 由子文档ID填充的mongoose,mongodb,mongoose,Mongodb,Mongoose,假设以下模式: Aschema ASchema = { type: { type: ObjectId, ref: BModel } } /** * ADoc = { * _id: 00a1, * type: 00b1001 * }, * */ BSchema = { children: [{ name: String, }] } /** * BDoc = {
ASchema = {
type: { type: ObjectId, ref: BModel }
}
/**
* ADoc = {
* _id: 00a1,
* type: 00b1001
* },
*
*/
BSchema = {
children: [{
name: String,
}]
}
/**
* BDoc = {
* _id: 00b1,
* children: [{
* _id: 00b1001,
* name: 'b1'
* }, {
* _id: 00b1002,
* name: 'b2'
* }]
* }
*
*/
BSchema
ASchema = {
type: { type: ObjectId, ref: BModel }
}
/**
* ADoc = {
* _id: 00a1,
* type: 00b1001
* },
*
*/
BSchema = {
children: [{
name: String,
}]
}
/**
* BDoc = {
* _id: 00b1,
* children: [{
* _id: 00b1001,
* name: 'b1'
* }, {
* _id: 00b1002,
* name: 'b2'
* }]
* }
*
*/
当我找到by_id:“00a1”时,我将得到以下结果
AModel.find({_id: '00a1'}).lean();
/**
* {
* _id: 00a1,
* type: 00b1001
* }
*/
如您所见,my ASchema type ref引用了B document中子文档的ObjectId
我想得到这个结果
/**
* {
* _id: 00a1,
* type: {
* _id: 00b1001,
* name: 'b1'
* }
* }
*/
我知道有,但我得到空,希望得到大家的帮助,谢谢
我发布了同样的问题,但描述不够清楚。
mongoose应该如何理解您想要id为00b1001的子文档?谢谢@SuleymanSah,mongodb子文档具有唯一id。AModel的类型保存了B的子文档唯一id