MongoDb如何将文档的id更改为不同的

发布于 2025-01-17 18:42:35 字数 1117 浏览 0 评论 0原文

您好,当某些ID相等时,如何将文档ID更改为Diffent? 我想将一些员工更改为id_director到对象ID等于id_director。我的意思是,例如,当某些文档具有id_director = 100时,然后将其更改为_id,其中id_employeee = 100

我当时尝试过这样的尝试:

var employes = db.employees.find({"id_director": {$ne: null}});
while (employes.hasNext()) {
emp = employes.next();
employe = db.employees.findOne({"id_director":emp.id_employe});


emp.id_director = employe._id

db.employees.save(emp)

}

例如,

我在一个集合中有两个文档:

employees
{
    "_id" : ObjectId("6224a5767b9cdbdcb681b1ef"),
    "id_employe" : 180,
    "id_director" : 100,
    "name" : "Mark"
}

{
    "_id" : ObjectId("6224a5767b9cdbdcb681b1f0"),
    "id_employe" : 100,
    "id_director" : null,
     "name" : "Peter"
}

预期文档:

{
    "_id" : ObjectId("6224a5767b9cdbdcb681b1ef"),
    "id_employe" : 180,
    "id_director" : ObjectId("6224a5767b9cdbdcb681b1f0"),
    "name" : "Mark"
}

Hello how to change an id of document to diffrent when some ids are equal?
I want to change some of employees id_director to object id thats is equal to id_director. I mean for example, when some document have id_director = 100 then changed it to _id with value of employee where id_employee = 100.

I was trying like this:

var employes = db.employees.find({"id_director": {$ne: null}});
while (employes.hasNext()) {
emp = employes.next();
employe = db.employees.findOne({"id_director":emp.id_employe});


emp.id_director = employe._id

db.employees.save(emp)

}

For example

I have two documents in one collection:

employees
{
    "_id" : ObjectId("6224a5767b9cdbdcb681b1ef"),
    "id_employe" : 180,
    "id_director" : 100,
    "name" : "Mark"
}

{
    "_id" : ObjectId("6224a5767b9cdbdcb681b1f0"),
    "id_employe" : 100,
    "id_director" : null,
     "name" : "Peter"
}

Expected document:

{
    "_id" : ObjectId("6224a5767b9cdbdcb681b1ef"),
    "id_employe" : 180,
    "id_director" : ObjectId("6224a5767b9cdbdcb681b1f0"),
    "name" : "Mark"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2025-01-24 18:42:35

首先你需要有 Peter 的 id 然后更新标记文档:

var peter= db.employees.findOne({_id: peterId});
db.employees.updateOne({
    _id: markId
}, {
    $set: {
        id_director: peter._id
    }
})

first of all you need to have peter's id then update mark document:

var peter= db.employees.findOne({_id: peterId});
db.employees.updateOne({
    _id: markId
}, {
    $set: {
        id_director: peter._id
    }
})
清欢 2025-01-24 18:42:35

您可以执行一个自我$查找以查找导演,然后执行一些争吵,$ MERGE返回集合。

db.collection.aggregate([
  {
    "$match": {
      "id_director": {
        $ne: null
      }
    }
  },
  {
    "$lookup": {
      "from": "collection",
      "localField": "id_director",
      "foreignField": "id_employe",
      pipeline: [
        {
          "$limit": 1
        },
        {
          $project: {
            _id: 1
          }
        }
      ],
      "as": "id_director"
    }
  },
  {
    $unwind: "$id_director"
  },
  {
    $project: {
      _id: 1,
      id_employe: 1,
      id_director: "$id_director._id",
      name: 1
    }
  },
  {
    "$merge": {
      "into": "collection",
      "on": "_id",
      "whenMatched": "replace"
    }
  }
])

这是 mongo playground 供您参考。

You can perform a self $lookup to lookup for the director_id, then perform some wrangling and $merge back to the collection.

db.collection.aggregate([
  {
    "$match": {
      "id_director": {
        $ne: null
      }
    }
  },
  {
    "$lookup": {
      "from": "collection",
      "localField": "id_director",
      "foreignField": "id_employe",
      pipeline: [
        {
          "$limit": 1
        },
        {
          $project: {
            _id: 1
          }
        }
      ],
      "as": "id_director"
    }
  },
  {
    $unwind: "$id_director"
  },
  {
    $project: {
      _id: 1,
      id_employe: 1,
      id_director: "$id_director._id",
      name: 1
    }
  },
  {
    "$merge": {
      "into": "collection",
      "on": "_id",
      "whenMatched": "replace"
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文