Contoh dokumen saya

  {
    _id: "bmasndvhjbcw",
    name: "lucas",
    occupation: "scientist",
    present_working:true,
    age: 55,
    location: "texas",

  },
  {
    _id: "bmasndvhjbcx",
    name: "mark",
    occupation: "scientist",
    age: 45,
    present_working:true,
    location: "texas",
  },
  {
    _id: "bmasndvhjbca",
    name: "stuart",
    occupation: "lab assistant",
    age: 25,
    location: "texas",
  },
  {
    _id: "bmasndvhjbcq",
    name: "cooper",
    occupation: "physicist",
    age: 69,
    location: "texas"
  }
]

Untuk catatan yang tidak memiliki present_working:true perlu menambahkan present_working:false

Seperti ini

  {
    _id: "bmasndvhjbcw",
    name: "lucas",
    occupation: "scientist",
    present_working:true,
    age: 55,
    location: "texas",

  },
  {
    _id: "bmasndvhjbcx",
    name: "mark",
    occupation: "scientist",
    age: 45,
    present_working:true,
    location: "texas",
  },
  {
    _id: "bmasndvhjbca",
    name: "stuart",
    occupation: "lab assistant",
    age: 25,
    present_working:false
    location: "texas",
  },
  {
    _id: "bmasndvhjbcq",
    name: "cooper",
    occupation: "physicist",
    age: 69,
    present_working:false,
    location: "texas"
  }
]
-1
sigmoid nuetron 23 Maret 2020, 11:33

1 menjawab

Jawaban Terbaik

Anda dapat menggunakan salah satu dari ini sesuai kebutuhan Anda:

db.collection.aggregate( [
  { $match: { present_working: { $exists: false } } },
  { $addFields: { present_working: false } }
] )

db.collection.aggregate( [
  { $addFields: { present_working: { $ifNull: [ "$present_working", false ] } } }
] )

Agregasi pertama hanya mengembalikan dokumen dengan bidang yang baru ditambahkan. Yang kedua memiliki dokumen, dengan dan tanpa bidang tambahan.

0
prasad_ 23 Maret 2020, 09:58