Mongo更新当前数据,重用数据集中的字段

发布于 2024-12-17 16:07:03 字数 418 浏览 2 评论 0原文

我的数据集在数据库中是完整的;但是我想在数据库中的每个文档上创建一个新字段。我想通过我的一些输入以及数据库中当前的其他字段导出这个新字段:

IE:

Document: 
{ 
   "_id":myId,
   "city":"fooville",
   "state":"bar"
}

然后我想获取并迭代每个条目并添加如下内容:

Document:
{
   "_id":myId,
   "city":"fooville",
   "state":"bar",
   "cityState":"fooville, bar"
}

有没有一种简单的方法可以做到这一点?尝试避免重新插入整个数据集。

预先感谢您的帮助

(Mongo 很棒)

My dataset is complete in the DB; however I want to create a new field on each of the documents in the db. This new field I want to be derived by some of my input along with other fields that are currently in the database:

IE:

Document: 
{ 
   "_id":myId,
   "city":"fooville",
   "state":"bar"
}

Then I want to take and iterate through every entry and add something like this:

Document:
{
   "_id":myId,
   "city":"fooville",
   "state":"bar",
   "cityState":"fooville, bar"
}

Is there an easy way to do this? Trying to avoid re-inserting the entire dataset.

Thank you in advance for your help

(Mongo is awesome)

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

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

发布评论

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

评论(1

街角卖回忆 2024-12-24 16:07:03

像这样的事情:

$results = $collection->find();

// iterate through the results
foreach ($results as $results) 
{

    $collection->update(array("_id" => new MongoId($result['_id'])), 
                        array('$set' => array("cityState" => sprintf("%s, %s", $result['city'], $result['state']))));

}

我还没有测试过......但它应该有效......

Something like this:

$results = $collection->find();

// iterate through the results
foreach ($results as $results) 
{

    $collection->update(array("_id" => new MongoId($result['_id'])), 
                        array('$set' => array("cityState" => sprintf("%s, %s", $result['city'], $result['state']))));

}

I haven't tested it....but it should work...

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