Hướng dẫn add field to object mongodb - thêm trường vào đối tượng mongodb

Giống như trường Cập nhật trường Bộ sưu tập hiện có,

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
9 sẽ thêm một trường mới nếu trường được chỉ định không tồn tại.

Kiểm tra ví dụ này:

> db.foo.find()
> db.foo.insert({"test":"a"})
> db.foo.find()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
> item = db.foo.findOne()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
> db.foo.update({"_id" :ObjectId("4e93037bbf6f1dd3a0a9541a") },{$set : {"new_field":1}})
> db.foo.find()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "new_field" : 1, "test" : "a" }

EDIT:

Trong trường hợp bạn muốn thêm New_Field vào tất cả bộ sưu tập của mình, bạn phải sử dụng bộ chọn trống và đặt Multi Flag thành True (param cuối cùng) để cập nhật tất cả các tài liệu

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)

EDIT:

Trong ví dụ trên, 2 trường cuối cùng

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
0 chỉ định các cờ
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
1 và
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
2.

UPSERT: Nếu được đặt thành True, hãy tạo một tài liệu mới khi không có tài liệu nào khớp với các tiêu chí truy vấn. If set to true, creates a new document when no document matches the query criteria.

Multi: Nếu được đặt thành True, hãy cập nhật nhiều tài liệu đáp ứng các tiêu chí truy vấn. Nếu được đặt thành sai, cập nhật một tài liệu. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.

Đây là dành cho Mongo

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
3 trước
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
4. Đối với các phiên bản mới nhất, truy vấn được thay đổi một chút

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5

Thêm các trường mới vào tài liệu.

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 Các tài liệu đầu ra có chứa tất cả các trường hiện có từ các tài liệu đầu vào và các trường mới được thêm vào.

Giai đoạn

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 tương đương với giai đoạn
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
8 chỉ định rõ ràng tất cả các trường hiện có trong các tài liệu đầu vào và thêm các trường mới.
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5
stage is equivalent to a
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
8 stage that explicitly specifies all existing fields in the input documents and adds the new fields.

Ghi chú

Bắt đầu từ phiên bản 4.2, MongoDB thêm một giai đoạn đường ống tổng hợp mới

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
9 là bí danh cho
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 có hình thức sau: has the following form:

{ $addFields: { : , ... } }

Chỉ định tên của mỗi trường để thêm và đặt giá trị của nó thành một biểu thức tổng hợp. Để biết thêm thông tin về biểu thức, xem biểu thức.

Quan trọng

Nếu tên của trường mới giống như tên trường hiện có (bao gồm

{ $addFields: { : , ... } }

2),
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 ghi đè giá trị hiện có của trường đó với giá trị của biểu thức được chỉ định.

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 nối các trường mới vào các tài liệu hiện có. Bạn có thể bao gồm một hoặc nhiều giai đoạn
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 trong một hoạt động tổng hợp.

Để thêm trường hoặc trường vào các tài liệu nhúng (bao gồm các tài liệu trong mảng) sử dụng ký hiệu dấu chấm. Xem ví dụ.example.

Để thêm một phần tử vào trường mảng hiện có với

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5, hãy sử dụng với

{ $addFields: { : , ... } }

7. Xem ví dụ.
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5
, use with

{ $addFields: { : , ... } }

7. See example.

Một bộ sưu tập có tên

{ $addFields: { : , ... } }

8 chứa các tài liệu sau:

{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}

Hoạt động sau sử dụng hai giai đoạn

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 để bao gồm ba trường mới trong các tài liệu đầu ra:
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5
stages to include three new fields in the output documents:

db.scores.aggregate( [
{
$addFields: {
totalHomework: { $sum: "$homework" } ,
totalQuiz: { $sum: "$quiz" }
}
},
{
$addFields: { totalScore:
{ $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
] )

Hoạt động trả về các tài liệu sau:

{
"_id" : 1,
"student" : "Maya",
"homework" : [ 10, 5, 10 ],
"quiz" : [ 10, 8 ],
"extraCredit" : 0,
"totalHomework" : 25,
"totalQuiz" : 18,
"totalScore" : 43
}
{
"_id" : 2,
"student" : "Ryan",
"homework" : [ 5, 6, 5 ],
"quiz" : [ 8, 8 ],
"extraCredit" : 8,
"totalHomework" : 16,
"totalQuiz" : 16,
"totalScore" : 40
}

Sử dụng ký hiệu DOT để thêm các trường mới vào các tài liệu nhúng.

Ví dụ: tạo một bộ sưu tập có tên

{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
0 với các tài liệu sau:

db.vehicles.insertMany(
[
{ _id: 1, type: "car", specs: { doors: 4, wheels: 4 } },
{ _id: 2, type: "motorcycle", specs: { doors: 0, wheels: 2 } },
{ _id: 3, type: "jet ski" }
]
)

Hoạt động tổng hợp sau đây thêm một trường mới

{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
1 vào tài liệu nhúng
{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
2.

db.vehicles.aggregate( [
{
$addFields: {
"specs.fuel_type": "unleaded"
}
}
] )

Hoạt động trả về các kết quả sau:

{ _id: 1, type: "car",
specs: { doors: 4, wheels: 4, fuel_type: "unleaded" } }
{ _id: 2, type: "motorcycle",
specs: { doors: 0, wheels: 2, fuel_type: "unleaded" } }
{ _id: 3, type: "jet ski",
specs: { fuel_type: "unleaded" } }

Chỉ định một tên trường hiện có trong hoạt động

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 khiến trường ban đầu được thay thế.

Một bộ sưu tập có tên

{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
4 chứa tài liệu sau:

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
0

Hoạt động

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 sau đây chỉ định trường
{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
6.

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
1

Hoạt động trả về tài liệu sau:

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
2

Có thể thay thế một trường bằng một trường khác. Trong ví dụ sau, trường

{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
7 thay thế cho trường

{ $addFields: { : , ... } }

2.

Một bộ sưu tập có tên

{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
9 chứa các tài liệu sau:

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
3

Hoạt động AgGregration sau sử dụng

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 để thay thế trường

{ $addFields: { : , ... } }

2 của mỗi tài liệu bằng giá trị của trường
{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
7 và thay thế trường
{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
7 bằng giá trị tĩnh.

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
4

Hoạt động trả về như sau:

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
5

Tạo bộ sưu tập mẫu

{ $addFields: { : , ... } }

8 với các mục sau:

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
6

Bạn có thể sử dụng

db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 với biểu thức

{ $addFields: { : , ... } }

7 để thêm phần tử vào trường mảng hiện có. Ví dụ: thao tác sau sử dụng
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5 để thay thế trường
db.scores.aggregate( [
{
$addFields: {
totalHomework: { $sum: "$homework" } ,
totalQuiz: { $sum: "$quiz" }
}
},
{
$addFields: { totalScore:
{ $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
] )
8 bằng một mảng mới có các phần tử là mảng
db.scores.aggregate( [
{
$addFields: {
totalHomework: { $sum: "$homework" } ,
totalQuiz: { $sum: "$quiz" }
}
},
{
$addFields: { totalScore:
{ $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
] )
8 hiện tại được nối với một mảng khác chứa điểm mới
{
"_id" : 1,
"student" : "Maya",
"homework" : [ 10, 5, 10 ],
"quiz" : [ 10, 8 ],
"extraCredit" : 0,
"totalHomework" : 25,
"totalQuiz" : 18,
"totalScore" : 43
}
{
"_id" : 2,
"student" : "Ryan",
"homework" : [ 5, 6, 5 ],
"quiz" : [ 8, 8 ],
"extraCredit" : 8,
"totalHomework" : 16,
"totalQuiz" : 16,
"totalScore" : 40
}
0.
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5
with a

{ $addFields: { : , ... } }

7 expression to add an element to an existing array field. For example, the following operation uses
db.your_collection.update({},
                          {$set : {"new_field":1}},
                          {upsert:false,
                          multi:true}) 
5
to replace the
db.scores.aggregate( [
{
$addFields: {
totalHomework: { $sum: "$homework" } ,
totalQuiz: { $sum: "$quiz" }
}
},
{
$addFields: { totalScore:
{ $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
] )
8 field with a new array whose elements are the current
db.scores.aggregate( [
{
$addFields: {
totalHomework: { $sum: "$homework" } ,
totalQuiz: { $sum: "$quiz" }
}
},
{
$addFields: { totalScore:
{ $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
] )
8 array concatenated with another array containing a new score
{
"_id" : 1,
"student" : "Maya",
"homework" : [ 10, 5, 10 ],
"quiz" : [ 10, 8 ],
"extraCredit" : 0,
"totalHomework" : 25,
"totalQuiz" : 18,
"totalScore" : 43
}
{
"_id" : 2,
"student" : "Ryan",
"homework" : [ 5, 6, 5 ],
"quiz" : [ 8, 8 ],
"extraCredit" : 8,
"totalHomework" : 16,
"totalQuiz" : 16,
"totalScore" : 40
}
0.

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
7

Hoạt động trả về như sau:

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
8

Làm cách nào để thêm một trường trong MongoDB?

Để thêm trường hoặc trường vào các tài liệu nhúng (bao gồm các tài liệu trong mảng) sử dụng ký hiệu dấu chấm. Xem ví dụ. Để thêm một phần tử vào một trường mảng hiện có với $ addfields, hãy sử dụng với $ concatarrays. Xem ví dụ.use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays . See example.

Làm cách nào để thêm một trường vào một mảng các đối tượng trong MongoDB?

Trong MongoDB, toán tử Push $ được sử dụng để nối một giá trị được chỉ định vào một mảng.Nếu trường được đề cập không có trong tài liệu để cập nhật, toán tử Push $ thêm nó dưới dạng trường mới và bao gồm giá trị được đề cập làm yếu tố của nó.$push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element.

Làm cách nào để thêm một trường vào một tài liệu hiện có trong MongoDB bằng Java?

Làm thế nào để thêm các trường trong một mảng MongoDB bằng Java..
Giới thiệu.....
Điều kiện tiên quyết.....
Bộ dữ liệu MongoDB.....
Thiết lập quyền truy cập vào triển khai MongoDB.....
Thiết lập quyền truy cập vào cơ sở dữ liệu MongoDB.....
Thiết lập quyền truy cập vào Bộ sưu tập MongoDB.....
Thêm một tài liệu MongoDB mới trong tài liệu MongoDB hiện có ..

Phương pháp nào sau đây của MongoDB được sử dụng để loại bỏ tài liệu S khỏi bộ sưu tập?

Phương thức xóa () của MongoDB được sử dụng để xóa một tài liệu khỏi bộ sưu tập.loại bỏ () phương thức chấp nhận hai tham số.Một là tiêu chí xóa và thứ hai là cờ Justone.Tiêu chí xóa - (tùy chọn) Tiêu chí xóa theo tài liệu sẽ bị xóa.remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag. deletion criteria − (Optional) deletion criteria according to documents will be removed.