In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
First of all, array preservation is supported in MongoDB, and once array preservation is supported, you need to match the array data.
Example: save part of the array contents
Db.emp.insert ({"name": "Liu A", "sex": "male", "age": 35, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "mathematics", "English", "music", "politics"]})
Db.emp.insert ({"name": "Liu B", "sex": "male", "age": 35, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "Mathematics"]})
Db.emp.insert ({"name": "Liu C", "sex": "male", "age": 35, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "Mathematics", "English"]})
Db.emp.insert ({"name": "Liu D", "sex": "male", "age": 35, "sal": 8000, "loc": "Beijing", "course": ["Chinese", "mathematics", "politics"]})
Db.emp.insert ({"name": "Liu E", "sex": "male", "age": 35, "sal": 8000, "loc": "Beijing", "course": ["language", "politics"]})
> db.emp.find () .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be26")
"name": "Zhao Yi"
"sex": "male"
"age": 30
"sal": 1000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be27")
"name": "Qian er"
"sex": "female"
"age": 22
"sal": 5000
"loc": "Shanghai"
}
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"age": 40
"sal": 2000
"loc": "Shenzhen"
}
{
"_ id": ObjectId ("599108423268c8e84253be29")
"name": "Li Si"
"sex": "female"
"age": 30
"sal": 7000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 6400
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2b")
"name": "Wu Liu"
"sex": "male"
"age": 30
"sal": 2500
"loc": "Chongqing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2c")
"name": "Zheng Qi"
"sex": "female"
"age": 50
"sal": 4700
"loc": "Chengdu"
}
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
"Music"
"politics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b88")
"name": "Liu B"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b89")
"name": "Liu C"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b8a")
"name": "Liu D"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"politics"
]
}
{
"_ id": ObjectId ("599129a20184ff511bf02b8b")
"name": "Liu E"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"politics"
]
}
In this case, the data contains array contents, and then you need to judge the array data. You can use several operators: $all, $size, $slice, $elemMatch.
Example: inquire about people who take both Chinese and math courses
Now the contents of both arrays need to be saved, so use "{" $all ", [content 1, content 2.]}"
> db.emp.find ({"course": {"$all": ["Chinese", "Mathematics"]}}. Pretty ()
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
"Music"
"politics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b88")
"name": "Liu B"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b89")
"name": "Liu C"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b8a")
"name": "Liu D"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"politics"
]
}
Now all the personnel information displayed contains the contents of Chinese and mathematics. And if one content is missing, it will not be displayed.
Although the "$all" calculation can be used on arrays, it can also be used for a data match.
Example: query the person whose location is "Chengdu"
> db.emp.find ({"loc": {"$all": ["Chengdu"]}}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be2c")
"name": "Zheng Qi"
"sex": "female"
"age": 50
"sal": 4700
"loc": "Chengdu"
Since the array information is now stored in the collection, the array can use the index operation to define the index in a "key.index" way.
Example: query the second content in the course (index=1, the index subscript starts at 0) for mathematical information
> db.emp.find ({"course.1": "Mathematics"}) .pretty ()
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
"Music"
"politics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b88")
"name": "Liu B"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b89")
"name": "Liu C"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b8a")
"name": "Liu D"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"politics"
]
}
Example: ask to find out the people who only take two courses
Use "$size" for quantity control
> db.emp.find ({"course": {"$size": 2}}) .pretty ()
{
"_ id": ObjectId ("599129a00184ff511bf02b88")
"name": "Liu B"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a20184ff511bf02b8b")
"name": "Liu E"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"politics"
]
}
Found that in the data query, as long as the content meets the conditions, the contents of the array will be fully displayed. You want to control the number of returns, you can use "$slice" to control.
Example: return all people at the age of 35, but only two courses are required
> db.emp.find ({"age": 35}, {"course": {"$slice": 2}}) .pretty ()
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b88")
"name": "Liu B"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b89")
"name": "Liu C"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b8a")
"name": "Liu D"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a20184ff511bf02b8b")
"name": "Liu E"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"politics"
]
}
Now that you only get the information of the first two doors, you can also set a negative number to take out the last two doors of information.
> db.emp.find ({"age": 35}, {"course": {"$slice":-2}}) .pretty ()
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Music"
"politics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b88")
"name": "Liu B"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b89")
"name": "Liu C"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Mathematics"
"English"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b8a")
"name": "Liu D"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Mathematics"
"politics"
]
}
{
"_ id": ObjectId ("599129a20184ff511bf02b8b")
"name": "Liu E"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"politics"
]
}
Or just take out the information in the middle.
> db.emp.find ({"age": 35}, {"course": {"$slice": [1Magne2]}}) .pretty ()
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Mathematics"
"English"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b88")
"name": "Liu B"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Mathematics"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b89")
"name": "Liu C"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Mathematics"
"English"
]
}
{
"_ id": ObjectId ("599129a00184ff511bf02b8a")
"name": "Liu D"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Mathematics"
"politics"
]
}
{
"_ id": ObjectId ("599129a20184ff511bf02b8b")
"name": "Liu E"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"politics"
]
}
Of the two data set at this time, the first data represents the location of the start, and the second data represents the number.
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.