繁体   English   中英

如何防止猫鼬使用 ObjectID 插入而不是嵌入它

[英]How to prevent mongoose from inserting with ObjectID instead of embedding it

我有两个 MongoDB 集合,如下所示, (一个图书馆只有一本书用于演示目的)

// books
| _id             | author | title |
| ObjectID("yyy") | jean   | foo   |
| ObjectID("yyy") | paul   | bar   |
| ObjectID("yyy") | baz    | boo   |

// library
| _id             | name   | book            |
| ObjectID("xxx") | foobar | ObjectID("yyy") |
| ObjectID("xxx") | pagez  | ObjectID("yyy") |
| ObjectID("xxx") | booky  | ObjectID("yyy") |

我为这样的图书馆制作了一个架构

export const LibrarySchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  book: {
    _id: false,
    type: Schema.Types.ObjectId, // (1)
    ref: "Book" // yes, the schema is registered properly, it's called `Book`
  }
})

(1)我有 ObjectID 因为(如集合中所示)这本书是用 objectid 引用存储的,如果我将它们更改为 book 模式,它在填充时将不再找到它们。

现在,问题来了,我的目标是拥有一个嵌入所有图书馆及其书籍的原始收藏,如下所示:

// library_complete
| _id          | name   | book                            |
| ObjId("xxx") | foobar | { author: "jean", title: "foo"} |
| ObjId("xxx") | booky  | { author: "baz", title: "boo"}  |

(请不要问我为什么需要原始(嵌入式)集合,我已经复制了我的目标以尽可能易于理解,并且绝对有必要拥有原始(嵌入式)集合)

原始库集合的架构是这样的:

const RawLibrarySchema = new Schema({
  name: {
    type: String,
    required: true
  },
  book: {
    _id: false,
    type: BookSchema, // changing this to `Object` doesn't work either
  }
})

现在,在我想创建一本书时的服务中

const linkLibaryToBook = async (library, bookId) => {
  // some complex calculations to link library to books and populates it
  const libraryBook = await someComplexFunction(library, bookId); // in this function the book gets populated, see the return type below
  console.log(libraryBook);
  /* @returns
  libraryBook {
    name: "foobar",
    book: { author: "jean", title: "foo" },
  }
  */
  saveRawLibrary(libraryBook);
}

上面的返回类型正是我想要实现的,现在当我像下面这样写的时候

const saveRawLibrary = async (libraryObject) => {
  console.log(libraryObject);
  /* @returns
  libraryBook {
    name: "foobar",
    book: { author: "jean", title: "foo" },
  }
  // ^ this is as it should be ^
  */
  const _library = new libraryModel(libraryObject);
  console.log(_library);
  /* @returns
  _library {
    name: "foobar",
    book: "9023920dada032aa" // the objectId of the original book
  }
  // ^ this is what comes out ^
  */
}

突然 mongoose 将书的 ID 链接到图书馆而不是对象。 我只想像前两个控制台日志中显示的那样写出普通对象。 我也试过使用这样的普通创建:

const saveRawLibrary = async (libraryObject) => {
  await libraryModel.create(libraryObject);
}

但是,这完全相同,它只是用 id 引用它......有没有办法简单地插入一个对象? 如果你想看到真实的物体和结果,你可以点击这里

TL; 博士; 我有一个我想写掉的 JavaScript 对象,但是如果我这样做,它会神奇地分配 objectID 而不是原始对象。

试试这个方法。

const mongoose = require('mongoose');

const bookSchema = new mongoose.Schema({
    title: String,
    author: String,
    library: {
      type: mongoose.Types.ObjectId,
      ref: 'library'
    },
  })

const librarySchema = new mongoose.Schema({
    name: String,
    book: {
      type: mongoose.Types.ObjectId,
      ref: 'book'
    }
});

const Book = mongoose.model('Book', bookSchema);
const Library = mongoose.model('Library',librarySchema);

const book1 = new Book({
    _id: new mongoose.Types.ObjectId(),
    author: 'Ian Fleming',
    title: 'Casino Royale',
});

console.log(book1);

book1.save(function (err) {
    if (err) return handleError(err);
});

const library1 = new Library({
    name: 'AMsterdaam',
    book: book1._id    // assign the _id from the book
});

library1.save(function (err) {
    if (err) return handleError(err);
  });

 console.log(library1); 

输出

{
  _id: 5f6f2d7e734c4e285e399460,
  author: 'Ian Fleming',
  title: 'Casino Royale'
}
{
  _id: 5f6f2d7e734c4e285e399462,
  name: 'AMsterdaam',
  book: 5f6f2d7e734c4e285e399460
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM