![](/img/trans.png)
[英]Mongoose: how to prevent null values being added to array of 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.