为什么 mongoose save() 不使用作为嵌套模式的默认值更新现有文档?
Why mongoose save() doesn't update existing document with default values which are a nested schema?
我有以下架构猫鼬:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const getRating = ({ maxScore }) => ({
type: new Schema(
{
score: {
type: Number,
default: 0
},
maxScore: {
type: Number,
default: function() {
console.log('inside default')
return maxScore
}
}
},
{ _id: false }
)
})
const BrandRating = new Schema(
{
varietyOfProducts: {
overall: getRating({ maxScore: 18 }),
userFeedback: getRating({ maxScore: 9 }),
other: getRating({ maxScore: 9 })
}
{ _id: false }
const Brand = new Schema({
rating: {
type: BrandRating,
required: true
},
slug: String
)
我在 mongodb 中已经有很多 Brand
文档,现在我需要 rating
对象,它是一个嵌套模式,每个字段也是一个具有默认值的嵌套模式.所以现在我想 运行 遍历数据库中的所有 Brand
文档并保存它们以便应用默认值。但是,它们没有应用,我不确定问题是什么:
const [brand] = await Brand.find({ 'slug': 'test' })
await brand.save()
console.log('brand', brand) // doesn't have any of the new rating default values, logs { slug: 'test' }
// { slug: 'test' }
我也没有看到 console.log
语句甚至在 default
mongoose 函数中被调用。
在 @thammada.ts
的帮助下,我终于弄明白了如何设置默认值。关键要点是将一个空对象设置为 BrandRating
模式的默认值,并在 getRating
:
内的最低可能级别上定义实际默认值
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const getRating = ({ maxScore }) => ({
type: new Schema(
{
score: {
type: Number
},
maxScore: {
type: Number
}
},
{ _id: false }
),
default: {
score: 0,
maxScore
}
})
const BrandRating = new Schema(
{
varietyOfProducts: {
overall: getRating({ maxScore: 18 }),
userFeedback: getRating({ maxScore: 9 }),
other: getRating({ maxScore: 9 })
}
{ _id: false }
const Brand = new Schema({
rating: {
type: BrandRating,
required: true,
default: {}
},
slug: String
)
我有以下架构猫鼬:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const getRating = ({ maxScore }) => ({
type: new Schema(
{
score: {
type: Number,
default: 0
},
maxScore: {
type: Number,
default: function() {
console.log('inside default')
return maxScore
}
}
},
{ _id: false }
)
})
const BrandRating = new Schema(
{
varietyOfProducts: {
overall: getRating({ maxScore: 18 }),
userFeedback: getRating({ maxScore: 9 }),
other: getRating({ maxScore: 9 })
}
{ _id: false }
const Brand = new Schema({
rating: {
type: BrandRating,
required: true
},
slug: String
)
我在 mongodb 中已经有很多 Brand
文档,现在我需要 rating
对象,它是一个嵌套模式,每个字段也是一个具有默认值的嵌套模式.所以现在我想 运行 遍历数据库中的所有 Brand
文档并保存它们以便应用默认值。但是,它们没有应用,我不确定问题是什么:
const [brand] = await Brand.find({ 'slug': 'test' })
await brand.save()
console.log('brand', brand) // doesn't have any of the new rating default values, logs { slug: 'test' }
// { slug: 'test' }
我也没有看到 console.log
语句甚至在 default
mongoose 函数中被调用。
在 @thammada.ts
的帮助下,我终于弄明白了如何设置默认值。关键要点是将一个空对象设置为 BrandRating
模式的默认值,并在 getRating
:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const getRating = ({ maxScore }) => ({
type: new Schema(
{
score: {
type: Number
},
maxScore: {
type: Number
}
},
{ _id: false }
),
default: {
score: 0,
maxScore
}
})
const BrandRating = new Schema(
{
varietyOfProducts: {
overall: getRating({ maxScore: 18 }),
userFeedback: getRating({ maxScore: 9 }),
other: getRating({ maxScore: 9 })
}
{ _id: false }
const Brand = new Schema({
rating: {
type: BrandRating,
required: true,
default: {}
},
slug: String
)