In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to nest arrays in MONGODB. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something through the detailed introduction of this article.
Let's take an example, one pair less
If we have a customer, his address information, generally speaking is not the case of the house brother, the house sister-in-law, a person gets the real estate information, a finger of the hand is enough. So I define it here as a one-to-few relationship.
{
name: 'super_man',
id:'109838973',
addresses:[
{city:'Beijing', district:' Chaoyang District ',street:' xcecdieei'},
{city:'Shanghai', district:' Hongqiao District', street:'werdfsaw'}
]
}
First of all, we first clarify two points, any design has advantages and disadvantages, which are generally coexisting, did not say that my design is perfect, here why use arrays, the reason is that when querying, you can bring out address information together, and address information is generally not directly used as a query condition.
Bottom line: Such designs do not have to be queried separately to get the embedded information, but cannot access the embedded details as separate entities.
For a one-to-many example, we can assume that we have a sales system in which we have a lot of information about old customers, and every time they order products, they are also group purchases, and each product needs to have customer information about the purchase product.
We can suggest a collection to store customer information
At the same time, a collection of ordering products is established, and the user information of ordering a certain product is stored in a document.
The above is a design method for handling one-to-many MONGODB. The advantage of this design is that they are all updated faster as a separate document, but each query requires two steps to go, instead of obtaining the desired data through a query, such as the address information of all customers for a certain order.
Personally, MONGODB collection design, to consider the convenience of later query and data change convenience, too complex multi-level nested array, is not conducive to MONGODB query and analysis.
We use mongodb 3.6 as an anchor point,
Let's not consider which design is better, let's first look at what such a design brings to UDPATE and Find of data based on a well-formed design (the following information comes from information returned by a mature third-party interface, some sensitive data has been changed or overwritten), and then we can know that such a design is better for certain scenarios.
We have a document below where we want to change the record with the name yesyesyes in queryConditions to nono
We use the following statement to make the relevant changes, involving the $placeholder for arrays in MONGODB.
db.py_queryfraud.update(
{"documentno":"130532199001108012","data.cisReport.queryConditions.$. name":"yesyesyes"},
{$set:{"data.cisReport.0.queryConditions.$. name":"nono"}})
We can see that the name value of the nested array of the first symbolic condition has been changed to what we specified, while the other values of name are still yesyesyes
The use of wildcards is also limited by the scene.
1 is not allowed to have the $symbol in the upsert operation
The 2-position $operator cannot be used for queries that traverse multiple arrays, such as arrays nested in other arrays, because the replacement of the $placeholder is a single value
3 When used with the $unset operator, the positional $operator does not remove the matching element from the array, but sets it to null.
4 If the query matches an array using a negative operator (such as $ne,$not, or $nin), you cannot update values from that array using positional operators. However, if the negative part of the query is in the $elemMatch expression, you can update that field with the positional operator.
The above content is how to nest arrays in MONGODB. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.