[英]How to insert a dictionary to a list in mongodb?
I need to insert a dictionary to a list in a list in mongodb, 我需要将字典插入mongodb列表中的列表,
I have this db: 我有这个db:
{library_id:1, library_name:rj, library_books:[{book_name:r&d, page_num:312, book_tags:[]}, {book_name:java, page_num:122, book_tags:[]}]}
and I want to insert into book_tags according to fitting book_name 我想根据拟合book_name插入book_tags
so it will be like this (for book_name r&d)- 所以它会像这样(对于book_name r&d) -
{library_id:1, library_name:rj, library_books:[{book_name:r&d, page_num:312, book_tags:[{tag_name:study, tag_description:self learning}]},{book_name:java, page_num:122, book_tags:[]}]}
How can I do this? 我怎样才能做到这一点? Thanks! 谢谢!
You should to update the field. 您应该更新该字段。 This seems like this: 这看起来像这样:
db.mongodb.update_one(
{ 'library_books.book_name': your_book_name },
{ $push: { 'library_books.book_name': your_tags } }
)
Maybe you need to use $each
instead of $push
. 也许你需要使用$each
而不是$push
。 The difference is describing here . 不同之处在于此处 。
Read this answer too. 也阅读这个答案。 I think this is your way. 我想这是你的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.