当前位置:网站首页>Mongodb array operation
Mongodb array operation
2022-06-13 00:36:00 【A small snail in a big city】
1. Add an element to the end of the array
push Is that , If the specified key already exists , It adds an element to the end of an existing array , If not, a new array will be created
db.user.update({
“_id” : ObjectId(“4ffcb2ed65282ea95f7e3304”)},{
$push:{
“relationships”:{
“fname”:”xiong”,”lname”:”lan”}}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "deng",
"lname" : "pan"
},
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "dongren",
"lname" : "zeng"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
2. Delete an element at the end of the array
Do... On the array “ reduce ” operation , Can achieve pair array “ reduce ” There are two modifiers for the purpose ,pop and pull
db.user.update({
“_id” : ObjectId(“4ffcb2ed65282ea95f7e3304”)},{
$pop:{
“relationships”:1}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "deng",
"lname" : "pan"
},
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "dongren",
"lname" : "zeng"
},
]
3. Delete an element at the beginning of the array
What should I do to delete from the beginning of the array . It's simple , Put the above “1” Change to “-1”, It will reverse the operation
db.user.update({
“_id” : ObjectId(“4ffcb2ed65282ea95f7e3304”)},{
$pop:{
“relationships”:-1}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "dongren",
"lname" : "zeng"
}
]
4. Delete an element from the middle of the array
Want to delete the middle of the array . At this time ,$pull Come in handy
pull You can delete the data in the middle of the array . Here's one more thing to note ,pull All matched data will be deleted
db.user.update({
“_id” : ObjectId(“4ffcb2ed65282ea95f7e3304”)},{
$pull:{
“relationships”:{
“fname”:”dongren”,”lname”:”zeng”}}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
5. Filter element
1. We insert the same data into the array , It can be inserted into the array normally . And in the actual production environment , We don't want to see such a result ,
that , Here are two more modifiers that you can use :ne and addToSet.$ne Mainly used to judge , If there is this value in array , Do not insert ; No, just insert it
db.user.update({
“relationships.fname”:{
ne:"xiong"}},{
set:{
“fname”:”xiong”,”lname”:”lan”}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "xiong",
"lname" : "lan"
},
]
2.ddToSet Than ne Better to use , It can determine whether the data exists by itself , And it and each Use a combination of , You can also insert multiple data into an array at the same time , This is a ne It can't be done
db.user.update({
“_id” : ObjectId(“4ffcb2ed65282ea95f7e3304”)},{
addToSet:{
"relationships":{
each:[{
“fname”:”xiong”,”lname”:”lan”},
{
“fname”:”dongren”,”lname”:”zeng”}]}}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "xiong",
"lname" : "lan"
},
{
"fname" : "xiong",
"lname" : "lan"
},
{
"fname" : "dongren",
"lname" : "zeng"
}
]
In a modification statement , We want to insert... At the same time {“fname”:”xiong”,”lname”:”lan”},
{“fname”:”dongren”,”lname”:”zeng”} Two figures , But in the original array ,
{“fname”:”xiong”,”lname”:”lan”} This data already exists , So it just inserted the back one . We have achieved what we want . therefore , I personally prefer to use $addToSet
6. Operate on some of them
Sometimes an array has multiple values , And we just want to operate on some of them . If we copy down the whole document , That's too much trouble and stupid . Fortunately mongodb It provides us with two simple methods : By position or operator “$”. Let's take a look at how these two methods are used . The first is to operate through the array position . Arrays are all based on 0 At the beginning , You can use subscripts directly as keys to select elements . for example , We want to add the age key value pair to the first data of the array , We can do this :
db.user.update({
“_id” : ObjectId(“4ffcb2ed65282ea95f7e3304”)},{
$set:{
“relationships.0.age”:22}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"age" : 22,
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "deng",
"lname" : "pan"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
But in many cases , Without querying the document in advance, we don't know to modify the subscript of the elements of the array . Then the positioning operator “$” It's easy to use . It is used to locate the matching elements of the query document , And update . Let's see how it works :
db.user.update({
“relationships.fname”:”xiong”},{
set:{
"relationships..age”:22}})
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"age" : 22,
"fname" : "qiang",
"lname" : "he"
},
{
"age" : 22,
"fname" : "deng",
"lname" : "pan"
},
{
"age" : 22,
"fname" : "xiong",
"lname" : "lan"
}
]
7 mongodb in $ The role of
Definition
Locator $ To determine the position of an element to be updated in the array , Instead of specifying the position of the element in the array .
usage
1. Update the values in the array
1) Grammar format
{
"<array>.$": value}
2) Example
Create set students There are the following documents :
db.students.insert([
{
"_id" : 1, "grades" : [ 85, 80, 80 ] },
{
"_id" : 2, "grades" : [ 88, 90, 92 ] },
{
"_id" : 3, "grades" : [ 85, 100, 90 ] }
])
take grades The first value in the array is 80 Updated to 82, If you don't know the exact location of the first value , You can write like this :
db.students.updateOne(
{
_id: 1, grades: 80 },
{
$set: {
"grades.$" : 82 } }
)
Locator $ Like a placeholder , Match the first value that matches the query criteria .
2. Update the document in the array
Locators make it easy to update arrays that contain embedded documents . To get the field attribute value embedded in the document , Use the dot symbol after . Such as the following syntax format description
1) Grammar format
db.collection.update(
{
<query selector> },
{
<update operator>: {
"array.$.field" : value } }
)
2) give an example
hypothesis students Collection grades The array contains the following embedded documents :
{
_id: 4,
grades: [
{
grade: 80, mean: 75, std: 8 },
{
grade: 85, mean: 90, std: 5 },
{
grade: 85, mean: 85, std: 8 }
]
}
Use $ Locator to update grades Fields in the document in the array std Value , The updated document is the first match grade The value is 85 Documents . as follows :
db.students.updateOne(
{
_id: 4, "grades.grade": 85 },
{
$set: {
"grades.$.std" : 6 } }
)
The updated result is :
{
"_id" : 4,
"grades" : [
{
"grade" : 80, "mean" : 75, "std" : 8 },
{
"grade" : 85, "mean" : 90, "std" : 6 },
{
"grade" : 85, "mean" : 85, "std" : 8 }
]
}
边栏推荐
- Several interview questions in TCP three grips and four swings
- Apispace empty number detection API interface is free and easy to use
- 6.824 Lab 3A: Fault-tolerant Key/Value Service
- 天津银行周传凯:从 0 到 1,我的分布式数据库落地经验谈
- The difference between philosophy and Literature
- Basics of network security (1)
- Card constructions -- two points
- Arduino control tm1637 common positive four digit nixie tube
- Summary of openstack installation problems
- Some basic design knowledge
猜你喜欢
Basics of network security (1)
63. different paths II
MySQL lpad() and rpad() concatenate string functions with specified length
也许尘埃落地,我们才能想清楚互联网的本质
The origin of MySQL in bedtime stories
BUUCTF之BabyUpload[GXYCTF2019]
[GXYCTF2019]禁止套娃--详解
Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
MASA Auth - 从用户的角度看整体设计
BUUCTF之BabySQL[极客大挑战 2019]
随机推荐
Successfully installed opencv under delphixe
阿姨学代码续集:能力吊打大批程序员
Through MDC, you can easily track your scheduled task log
2022 constructor - Equipment direction - General Foundation (constructor) operation certificate examination questions and simulation examination
Paper reading and sharing
Buuctf's babysql[geek challenge 2019]
USTC of China University of science and technology: Minrui Wang | distribution network voltage stabilization based on transformer Multi-Agent Reinforcement Learning
After so long use, CSDN has finally opened a blog
String类中split()方法的使用
Building crud applications in golang
TypeError: wave. ensureState is not a function
Converting Chinese numbers to Arabic numbers in Delphi
BUUCTF之BabyUpload[GXYCTF2019]
[LeetCode]14. Longest common prefix thirty-eight
Arduino control tm1637 common positive four digit nixie tube
In / out / inout details of MySQL stored procedures
Mysql批量插入数据时如何解决重复问题?
杂记:intel11代和12代移动版支持原生Thunderbolt4接口,桌面版不支持
Kalix system - use of information collection gadgets
[Error] invalid use of incomplete type 使用了未定义的类型