The common mongodb update operators were described in detail in the mongodb update operators section. This article begins with the operators and operator-modifiers needed to manipulate arrays. In the MongoDB schema, we often store some data in an array type, which is an implementation of our common nested schema design. This approach to array design is unusual in relational databases. Operations on arrays can be divided into two categories: array operators and array operator modifiers.

1. Array operators

1.1 $a push

Description:

Adds a specific element or value to an array. Can work with

Grammar:

{ $push: { : , … }}

** Note: ** If the field is not present in the document to be updated, $push adds the value as an element to the array field.

If the field is not an array, the operation will fail.

If the value is an array, push appends the entire array as a single element. To append each element of the value separately, append the entire array as a single element using push. To append each element of the value separately, append the entire array as a single element using push. To add each element of value separately, use the push modifier $each.

1.1.1. The sample

1.1.1.1. Add a value to the array

Initialize data:

db.inventory.insertOne({ _id: 1, item: “polarizing_filter”, tags: [ ] })

Example: Adding a value to an array

db.inventory.update( { _id: 1 }, {$push: { tags: “test” } } )

Query after change:

db.inventory.findOne({“_id”:1})

Return result:

{ _id: 1, item: ‘polarizing_filter’, tags: [ ‘test’ ] }

1.1.1.2. Add multiple values to the array

Example: Adding multiple values to an array

db.inventory.update(

{ _id: 1},

{ push: { tags: { each: [ “test1”, “test”, “test2” ] } } }

)

Query after change:

db.inventory.findOne({“_id”:1})

Return result:

{ _id: 1,

item: ‘polarizing_filter’,

tags: [ ‘test’, ‘test1’, ‘test’, ‘test2’ ] }

1.2 $addToSet

Description:

AddToSet does not guarantee a particular order of the elements in the modified collection, if you use addToSet on a field that is not an array, If you use addToSet on a field that is not an array, the operation will fail. If the value is an array, $addToSet appends the entire array as a single element.

Grammar:

{ $addToSet: { : , … }}

1.2.1. The sample

Initialize data:

db.inventory.insertOne({ _id: 1, item: “polarizing_filter”, tags: [ “electronics”, “camera” ] })

** Note: ** will not be inserted if the inserted value already exists in the tags array.

1.2.1.1. Array fields add a single value

Example: Append accessories values to the Tags array field

db.inventory.update(

{ _id: 1 },

{ $addToSet: { tags: “accessories” } }

)

Query after change:

db.inventory.findOne({“_id”:1})

Return result:

{ _id: 1,

item: ‘polarizing_filter’,

tags: [ ‘electronics’, ‘camera’, ‘accessories’ ] }

1.2.1.2. Array fields add multiple values

Example: Append multiple values to the tags array field

db.inventory.update(

{ _id: 1},

{ addToSet: { tags: { each: [ “test”, “test1”, “test2” ] } } }

)

Query after change:

db.inventory.findOne({“_id”:1})

Return result:

{ _id: 1,

item: ‘polarizing_filter’,

tags: [ ‘electronics’, ‘camera’, ‘accessories’ ] }

1.3 $pop

Description:

The pop operator removes the first or last element of the array. Removes the first or last element of the array to the POP operator. Removes the first or last element of the array to the POP operator. Pop is passed a value -1 to remove the first element in the array and 1 to remove the last element in the array.

Grammar:

{ $pop: { : <-1 | 1>, … }}

** Note: $pop fails if it is not an array.

If the $POP operator deletes the last item in, an empty array is saved.

1.3.1. The sample

Initialize data:

db.inventory.insertOne({ _id: 1, item: “polarizing_filter”, tags: [ “electronics”, “camera” ] })

** Note: ** will not be inserted if the inserted value already exists in the tags array.

1.3.1.1. Delete the first element value of an array field

Example: Append accessories values to the Tags array field

db.inventory.update( { _id: 1 }, { $pop: { tags: -1 } } )

Query after change:

db.inventory.findOne({“_id”:1})

Return result:

{ _id: 1,

item: ‘polarizing_filter’,

tags: [ ‘camera’] }

1.3.1.2. Delete the last element value of an array field

Example: Append multiple values to the tags array field

db.inventory.update( { _id: 1 }, { $pop: { tags: 1 } } )

Query after change:

db.inventory.findOne({“_id”:1})

Return result:

{ _id: 1,

item: ‘polarizing_filter’,

tags: [ ] }

1.4 $a pull

Description:

The pull operator removes a value from an existing array or all array elements of a value that matches a specified condition. With the pull operator, you can remove a value from an existing array or all array elements that match a value that matches a specified condition. With the pull operator, you can remove a value from an existing array or all array elements that match a value that matches a specified condition. Can be used with each, position, position, position, slice, $sort

Grammar:

{ $pull: { : <value|condition>, : <value|condition>, … }}

{ $push: { : { : , … },… }}

1.4.1. Sample

1.4.1.1. Remove all elements equal to the specified value

Initialize data:

db.stores.insertMany([{

_id: 1,

fruits: [ “apples”, “pears”, “oranges”, “grapes”, “bananas” ],

vegetables: [ “carrots”, “celery”, “squash”, “carrots” ]

},

{

_id: 2,

fruits: [ “plums”, “kiwis”, “oranges”, “bananas”, “apples” ],

vegetables: [ “broccoli”, “zucchini”, “carrots”, “onions” ]

}])

Example: Update all documents in the collection by removing the “apples” and “oranges” from the array fruit and “Carrot” from the array vegetables:

db.stores.update(

{},

{ pull: { fruits: { in: [ “apples”, “oranges” ] }, vegetables: “carrots” } },

{ multi: true }

)

Query after change:

db.stores.find()

Return result:

{ _id: 1,

fruits: [ ‘pears’, ‘grapes’, ‘bananas’ ],

vegetables: [ ‘celery’, ‘squash’ ] }

{ _id: 2,

fruits: [ ‘plums’, ‘kiwis’, ‘bananas’ ],

vegetables: [ ‘broccoli’, ‘zucchini’, ‘onions’ ] }

1.4.1.2. Delete all entries that match the specified $pull condition

Initialize data:

db.profiles.insert({ _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] })

Example: The following operation removes all elements greater than or equal to ($gte) 6 from the votes array:

db.profiles.update( { _id: 1 }, { pull: { votes: { gte: 6 } } } )

Query after change:

db.profiles.find()

Return result:

{ _id: 1, votes: [ 3, 5 ] }

1.4.1.3. Remove elements from the document array

Initialize data:

db.survey.insertMany([{

_id: 1,

results: [

{ item: “A”, score: 5 },

{ item: “B”, score: 8, comment: “Strongly agree” }

]

},

{

_id: 2,

results: [

{ item: “C”, score: 8, comment: “Strongly agree” },

{ item: “B”, score: 4 }

]

}])

Example: Delete the element whose results field score equals 8 and item equals B

db.survey.update(

{},

{ $pull: { results: { score: 8 , item: “B” } } },

{ multi: true }

)

Query after change:

db.survey.find()

Return result:

{ _id: 1, results: [ { item: ‘A’, score: 5 } ] }

{ _id: 2,

results:

[ { item: ‘C’, score: 8, comment: ‘Strongly agree’ },

{ item: ‘B’, score: 4 } ] }

1.5 $pullAll

Description:

Removes all matching values from the array.

Grammar:

{ $pullAll: { : [ , … ] . }}

1.5.1. The sample

Initialize data:

db.testPullAll.insert({ _id: 1, scores: [ 0, 2, 5, 5, 1, 0 ] })

Example:

db.testPullAll.update( { _id: 1 }, { $pullAll: { scores: [ 0, 5 ] } } )

Query after change:

db.testPullAll.find()

Return result:

{ _id: 1, scores: [ 2, 1 ] }

2. Array operator modifier

2.1 $each

Description:

Used with push and push and push and addToSet to manipulate multiple values

Grammar:

{ addToSet: {

: { each: [ , … ] }}}

{ push: {

: { each: [ , … ] }}}

2.2 $slice

Description:

Used with push and push and push and each to reduce the size of the updated array.

Grammar:

{

$push: {

: {

$each: [ , , … ] .

$slice:

}

}

}

2.3 $sort

Description:

Sort the subdocuments in an array with push, push, push, each, $slice.

Grammar:

{

$push: {

: {

$each: [ , , … ] .

$sort:

}

}

}

2.4 $position

Description:

The position modifier specifies where the push operator inserts elements into the array. If you don’t have the position modifier, the position modifier, the position modifier, the push operator inserts the element at the end of the array, to use the position modifier, it has to match the position modifier, it has to match the position modifier, It must appear with the each modifier.

Grammar:

{

$push: {

: {

$each: [ , , … ] .

$position:

}

}

}

2.5. Use the $push example with multiple modifiers

Initialize data:

db.students.insert({

“_id” : 10,

“quizzes” : [

{ “wk”: 1, “score” : 10 },

{ “wk”: 2, “score” : 8 },

{ “wk”: 3, “score” : 5 },

{ “wk”: 4, “score” : 6 }

]

})

Example: The following $push operation uses:

Add multiple documents to the $each modifier of the Quizzes array,

The $sort modifier sorts all the elements of the modified Quizzes array in descending order by score field

The $slice modifier only preserves the first three sorted elements of the Quizzes array.

db.students.update(

{ _id: 10 },

{

$push: {

quizzes: {

$each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],

$position: 1,

$sort: { score: -1 },

$slice: 3

}

}

}

)

Query after change:

db.students.find({“_id”:10})

Return result:

{ _id: 10,

quizzes:

[ { wk: 1, score: 10 },

{ wk: 2, score: 8 },

{ wk: 5, score: 8 } ] }

The next mongodb remove operation