我有一个带有shelves
数组字段的用户文档,其中是带有shelf_id
和books
数组的对象。
给定books
后,如何添加到shelf_id
数组中? (最好使用Python,但语言无关紧要)
我的文档结构如下:
{
"firebase_id": "(id)",
"shelves": [{
"name": "To read",
"books": ["(book id)"],
"shelf_id": "(shelf id)"
}]
}
db.users.update({"firebase_id": "(id)"}, {"$push": {}})
感谢您的帮助!预先感谢!
答案 0 :(得分:1)
您要使用$arrayFilters
db.users.updateMany(
{'firebase_id': '(id)'},
{
$push: {
'shelves.$[elem].books': newBook,
}
},
{
arrayFilters: [
{'elem.shelf_id': shelfId}
]
});