Jef*_*man 1 multidimensional-array mongodb node.js
我有一个看起来像这样的 mongo 集合
db.users.find().pretty()
{
"_id" : ObjectId("57c3d5b3d364e624b4470dfb"),
"fullname" : "tim",
"username" : "tim",
"email" : "tim@gmail.com",
"password" : "$2a$10$.Z9CnK4oKrC/CujDKxT6YutohQkHbAANUoAHTXQp.73KfYWrm5dY2",
"workout" : [
{
"workoutId" : "Bkb6HIWs",
"workoutname" : "chest day",
"BodyTarget" : "Chest",
"date" : "Monday, August 29th, 2016, 2:27:04 AM",
"exercises" : [
{
"exerciseId" : "Bym88LZi",
"exercise" : "bench press",
"date" : "Monday, August 29th, 2016, 2:29:30 AM"
},
{
"exerciseId" : "ByU8II-s",
"exercise" : "flys",
"date" : "Monday, August 29th, 2016, 2:29:34 AM"
}
]
},
{
"workoutId" : "Bk_TrI-o",
"workoutname" : "Back day",
"BodyTarget" : "Back",
"date" : "Monday, August 29th, 2016, 2:27:12 AM"
}
]
}
Run Code Online (Sandbox Code Playgroud)
所以我最终希望它看起来像这样
db.users.find().pretty()
{
"_id" : ObjectId("57c3d5b3d364e624b4470dfb"),`enter code here`
"fullname" : "tim",
"username" : "tim",
"email" : "tim@gmail.com",
"password" : "$2a$10$.Z9CnK4oKrC/CujDKxT6YutohQkHbAANUoAHTXQp.73KfYWrm5dY2",
"workout" : [
{
"workoutId" : "Bkb6HIWs",
"workoutname" : "chest day",
"BodyTarget" : "Chest",
"date" : "Monday, August 29th, 2016, 2:27:04 AM",
"exercises" : [
{
"exerciseId" : "Bym88LZi",
"exercise" : "bench press",
"date" : "Monday, August 29th, 2016, 2:29:30 AM",
"stats" : [
{
"reps: '5',
"weight":'105'
}
},
{
"exerciseId" : "ByU8II-s",
"exercise" : "flys",
"date" : "Monday, August 29th, 2016, 2:29:34 AM"
}
]
},
{
"workoutId" : "Bk_TrI-o",
"workoutname" : "Back day",
"BodyTarget" : "Back",
"date" : "Monday, August 29th, 2016, 2:27:12 AM"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想将 stats 数组添加到当前的练习数组中。我在使用双嵌套数组的点表示法时遇到问题
我试过这个
db.users.update({
'email': 'jeffreyyourman@gmail.com', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
$push: {
"workout.0.exercises.$.stats": {"sets":"sets", "reps":"reps"}}})
Run Code Online (Sandbox Code Playgroud)
这实际上有效,但总是会推送到第一个嵌套的练习对象。
现在如果我这样做...
db.users.update({
'email': 'jeffreyyourman@gmail.com', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
$push: {
"workout.0.exercises.1.stats": {"sets":"sets", "reps":"reps"}}})
Run Code Online (Sandbox Code Playgroud)
并将 $ 替换为 1 它将推送到第二个练习数组,这显然是我想要的。但是我正在建立一个网站,所以我显然不能对其进行硬编码。我需要使用 $ 但它似乎没有让它通过第一个练习对象。
任何帮助我将不胜感激!
小智 5
现在(MongoDB >=3.6)有一种方法可以使用arrayFilters和$[identifier]来做到这一点。
下面的示例使用 mongoose 并将一个项目添加到双嵌套数组内的数组中。一个很好的文章,解释这是在这里。
const blogPost = await BlogPost.create({
title : 'A Node.js Perspective on MongoDB 3.6: Array Filters',
comments : [
{ author : 'Foo', text : 'This is awesome!', replies : { name : 'George', seenBy : ['Pacey'] } },
{ author : 'Bar', text : 'Where are the upgrade docs?', replies : { name : 'John', seenBy : ['Jenny'] } }
]
});
const updatedPost = await BlogPost.findOneAndUpdate({ _id : blogPost._id }, {
$addToSet : {
'comments.$[comment].replies.$[reply].seenBy' : 'Jenny'
}
}, {
arrayFilters : [{ 'comment.author' : 'Foo' }, { 'reply.name' : 'George' }],
new : true
});
console.log(updatedPost.comments[0].replies);
Run Code Online (Sandbox Code Playgroud)