我有一个带有api的nodejs express应用程序,用于从mongodb数据库返回数据。这是我的猫鼬模型:
const bookingSchema = new mongoose.Schema({
timestamp: {
type: Date,
default: Date.now,
required: true
},
tags: {
type: [String],
required: true
},
amount: {
type: Number,
required: true
},
type: {
type: String,
required: true,
enum: ['expense', 'income']
}
})
当我使用路径/api/bookings/listbymonth/2019/1
调用api时,将在后端内部调用此函数:
const bookingsListByMonth = (req, res) => {
const year = ("0000" + req.params.year).slice(-4)
const month = ("0000" + req.params.month).slice(-2)
const dateOfMonth = `${year}${month}01`
const start = moment(dateOfMonth).startOf("month")
const end = moment(dateOfMonth).endOf("month")
bookingMongooseModel
.find({
timestamp: {
$gt: start,
$lt: end
}
})
.sort({ timestamp: 1 })
.exec((err, bookings) => {
if (!bookings) {
return res
.status(404)
.json({
"message": "booking not found"
})
} else if (err) {
return res
.status(404)
.json(err)
}
res
.status(200)
.json(processBookings(bookings));
})
}
我不仅要简单地返回json数据,还想对数据进行预处理,并制作一个漂亮的时间戳和货币字段。这就是json数据通过附加的processBookings
函数运行的原因。为了进行测试,我尝试添加另一个字段timestamp2: 123
:
const processBookings = (bookings) => {
console.log("Bookings unsorted: \n" + bookings + "\n")
const mainTags = [
"Essen",
"Essen gehen",
"Notwendiges",
"Luxus",
]
let bookingsProcessed = []
mainTags.forEach((tag) => {
let singleTagBookings = bookings.filter(
booking => booking.tags.includes(tag)
)
singleTagBookings.map((item) => {
item.timestamp2 = "123"
return item
})
let message = null;
if (singleTagBookings.length === 0) {
message = "No bookings found";
}
bookingsProcessed.push({
name: tag,
bookings: singleTagBookings,
message: message
})
});
console.log("Bookings sorted:")
bookingsProcessed.forEach((item) => {
console.log(item)
})
return bookingsProcessed
}
bookings
数组中的对象应具有另一个属性timestamp2: "123"
,但没有。输出如下:
Bookings unsorted:
{ tags: [ 'Luxus', 'voluptatem', 'atque', 'qui', 'sunt' ],
_id: 5cb2c9e1ff6c9c6bef95f56f,
timestamp: 2019-01-06T08:53:06.945Z,
amount: 68.02,
type: 'expense',
__v: 0 },{ tags: [ 'Essen gehen', 'ut', 'unde', 'et', 'officiis' ],
_id: 5cb2c9e1ff6c9c6bef95f56e,
timestamp: 2019-01-09T20:35:06.411Z,
amount: 33.77,
type: 'income',
__v: 0 }
Bookings sorted:
{ name: 'Essen', bookings: [], message: 'No bookings found' }
{ name: 'Essen gehen',
bookings:
[ { tags: [Array],
_id: 5cb2c9e1ff6c9c6bef95f56e,
timestamp: 2019-01-09T20:35:06.411Z,
amount: 33.77,
type: 'income',
__v: 0 } ],
message: null }
{ name: 'Notwendiges',
bookings: [],
message: 'No bookings found' }
{ name: 'Luxus',
bookings:
[ { tags: [Array],
_id: 5cb2c9e1ff6c9c6bef95f56f,
timestamp: 2019-01-06T08:53:06.945Z,
amount: 68.02,
type: 'expense',
__v: 0 } ],
message: null }
正如评论中所建议的那样,我尝试使用let bookings = [ {tags: ["Essen"]}];
作为测试数据。在这里工作。输出为:
Bookings unsorted:
[object Object]
Bookings sorted:
{ name: 'Essen',
bookings: [ { tags: [Array], timestamp2: '123' } ],
message: null }
{ name: 'Essen gehen',
bookings: [],
message: 'No bookings found' }
{ name: 'Notwendiges',
bookings: [],
message: 'No bookings found' }
{ name: 'Luxus', bookings: [], message: 'No bookings found' }
所以我想这与我的猫鼬模型有关,后者限制添加任何其他字段。但是如果我放
console.log("EXTENSIBLE " + Object.isExtensible(bookings))
res
.status(200)
.json(processBookings(bookings));
进入我的bookingsListByMonth
函数:
EXTENSIBLE true
因此,从理论上讲,我应该能够向bookings
对象中添加一些内容?
作为一种解决方法,我在猫鼬模型中添加了timestamp2
字段:
const bookingSchema = new mongoose.Schema({
timestamp: {
type: Date,
default: Date.now,
required: true
},
timestamp2: {
type: String,
default: null
},
tags: {
type: [String],
required: true
},
amount: {
type: Number,
required: true
},
type: {
type: String,
required: true,
enum: ['expense', 'income']
}
})
这有效,但是它向我的数据库中添加了一个额外的无用数据字段。如何修改从mongodb返回的bookings
json对象?如果由于它是猫鼬模型而无法修改它,该如何制作可编辑的副本?
答案 0 :(得分:1)
从查询返回的文档是猫鼬文档,它限制了新字段的添加。为了将猫鼬文档转换为纯JavaScript对象,猫鼬中有一个方法/选项 lean -https://mongoosejs.com/docs/tutorials/lean.html。
查找查询看起来像这样
bookingMongooseModel
.find({
timestamp: {
$gt: start,
$lt: end
}
})
.sort({ timestamp: 1 })
.lean()
此外,猫鼬提供了可配置的选项,供您在定义架构时进行设置。例如,new Schema({..},{timestamps:true});会在您的文档createdAt和updatedAt中创建两个字段。有关更多信息,请参见https://mongoosejs.com/docs/guide.html#options
的“选项”部分答案 1 :(得分:1)
只需使用toObject()函数将猫鼬对象转换为标准javascript对象即可。然后,您将能够修改数据/添加新字段/更改数据类型:
const processBookings = (bookings) => {
bookings = bookings.map(function(e){return e.toObject();});
...
答案 2 :(得分:0)
singleTagBookings.map((item) => {
item.timestamp2 = "123"
return item
})
我认为这是您的问题。
所以尝试这个
singleTagBookings = singleTagBookings.map((item) => ({
...item,
timestamp2: "123"
}))