gpt4 book ai didi

javascript - 更新数据库上深度嵌套数据的正确方法是什么

转载 作者:行者123 更新时间:2023-12-02 23:08:43 25 4
gpt4 key购买 nike

我有以下数据库结构:

{
user: {
email: "example@email.com",
days: {
day1: {
tasks: {
task1: {
name: 'sometask1,
state: 'false'
}
task2: {
name: 'sometask2,
state: 'false'
}
task3: {
name: 'sometask3,
state: 'false'
}
...
}
...
}
}
}
}

我的架构:

// user Schema 

const userSchema = new mongoose.Schema({
email: String,
password: String,
username : String,
displayUserName : String,
days : [{
type : Object,
ref : Day
}]
});

// day schema

const daySchema = new mongoose.Schema({
dayProgress: Number,
totalTodos: Number,
tasks: [{
type: Object,
ref: Task,
}]
});

// task secmea

const taskSchema = new mongoose.Schema({
taskName: {
type: String,
required: true
},
taskTiming: {
type: Date,
required: true
},
taskState: {
type: Boolean,
default: false
},
taskTypes: {
type: Object,
default: {}
}
});


const User = mongoose.model('User',userSchema)
const Day = mongoose.model('Day',daySchema)
const Task = mongoose.model("Task", taskSchema)

现在假设用户想要添加一个新任务...更新 tasks 对象并防止整个数组的覆盖行为的最佳方法是什么。

到目前为止我尝试过的是这个......但它覆盖了整个 tasks 数组:

const task = new Task({
taskName: 'test',
taskTiming: new Date(),
taskState: false,
})

const day = new Day({
dayProgress: 0,
totalTodos: 0,
tasks: task
})

const dayObj = {}
dayObj['day 1'] = day

db.findOneAndUpdate({
email: 'example@email.com'
}, {
$set: dayObj
})

我得到了什么:

{
user: {
email: "example@email.com",
days: {
day1: {
tasks: {
0: {
name: 'test',
state: 'false'
...
}
// each time i add a task it gets overwritten
}
}
}
}
}

我在寻找什么:

{
user: {
email: "example@email.com",
days: {
day1: {
tasks: {
test : {
name: 'test',
state: 'false'
...
}
// i can push more tasks here
}
}
}
}
}

我知道我应该将值推送到 tasks 数组或类似的东西,但我不太确定该怎么做,谢谢您的时间

最佳答案

您可以尝试以下查询:(使用 $push$ 推送到确切位置)

db.update({
_id: ObjectId("5d51bd5ef5fc3d6486b40ffb"),
"days._id": ObjectId("5d51bd0ff5fc3d6486b40fe4")
}, {
$push: {'days.$.tasks': taskData}
});

Note:- I have created manually data and tested below query. I have used _id and day._id to find the user and the day where the task needs to push.

之前

{
"_id" : ObjectId("5d51bd5ef5fc3d6486b40ffb"),
"email" : "kk@kk.com",
"username" : "KK_KK",
"displayUserName" : "KK",
"days" : [
{
"_id" : ObjectId("5d51bd0ff5fc3d6486b40fe4"),
"dayProgress" : 1,
"totalTodos" : 2,
"tasks" : [
{
"_id" : ObjectId("5d51bcdef5fc3d6486b40fd2"),
"taskName" : "ABC",
"taskState" : false,
"taskTypes" : "NEW"
}
]
}
]
}

之后

{
"_id" : ObjectId("5d51bd5ef5fc3d6486b40ffb"),
"email" : "kk@kk.com",
"username" : "KK_KK",
"displayUserName" : "KK",
"days" : [
{
"_id" : ObjectId("5d51bd0ff5fc3d6486b40fe4"),
"dayProgress" : 1,
"totalTodos" : 2,
"tasks" : [
{
"_id" : ObjectId("5d51bcdef5fc3d6486b40fd2"),
"taskName" : "ABC",
"taskState" : false,
"taskTypes" : "NEW"
},
{
"taskName" : "ABC1",
"taskState" : true,
"taskTypes" : "OLD"
}
]
}
]
}

希望这有帮助!

关于javascript - 更新数据库上深度嵌套数据的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57466878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com