从不同的数据库更新集合 [英] Updating a collection from a different database

查看:30
本文介绍了从不同的数据库更新集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mongo 4.1 并希望更新名为location_copy"的集合,方法是向其添加一个名为time"的类型对象的新字段,其中包含两个子字段:utcTime",它将是由该文档时间"的值填充字段和tz",它们将由subject.contactInf[0].addresses[0].timeZoneID"的值填充;from 集合subjects"中的文档在数据库主题"中(与第一个集合中的数据库不同的数据库)带有_id";对应于subjectID"的字段值location_copy 中的字段.

I'm using Mongo 4.1 and would like to update a collection named "location_copy", by adding a new field to it of type object named "time", with two subfields: "utcTime", which will be populated by the value of that documents "time" field, and "tz", which will be populated by value of "subject.contactInf[0].addresses[0].timeZoneID" from of the document in the collection "subjects" in the database "Subjects" (a different database from the one of the first collection) with "_id" field value corresponding to "subjectID" field in locations_copy.

我尝试使用以下代码完成此操作:

I have tried to accomplish this with the following code:

const get_time_zone_id = function(doc) {return doc.contactInfo[0].addresses[0].timeZoneID}
const get_location_doc = function(subjectID) {    return db.getSiblingDB('Subjects').subjects.find({"_id": subjectID, "contactInfo": {"$exists": true}, "$where" : function() {

   return (this.contactInfo.length > 0 && this.contactInfo[0].addresses && this.contactInfo[0].addresses.length > 0 && this.contactInfo[0].addresses[0].timeZoneID)
}}, {"contactInfo" : {"$slice": 1}, "contactInfo.addresses": {"$slice": 1},"contactInfo.addresses.timeZoneID" : 1}).map(get_time_zone_id)}

db.locations_copy.aggregate( [
   { $match: {"subjectID": {"$exists": true}}},
   { $addFields: {
       time: { utc: "$timeUTC",
               tz: { "$arrayElemAt": [get_location_doc(ObjectId("$subjectID")), 0 ] }}
       
     }
   }
] ).forEach(function(x){db.locations_copy.save(x)})

除了一件事之外,一切正常:当我尝试将 ObjectId($subjectID") 作为参数传递给get_location_doc"时,它解析$subjectID";作为文字字符串,而不是在每个文档中传递基础字段的值.我也试过简单地传递 subjectID(不带引号),在这种情况下它只是未定义,或者$$subjectID"这使我再次使用文字字符串.我知道这是由于客户端/服务器端在运行时解析造成的.我曾尝试使用$function"运算符,但显然它仅适用于 4.4 版(我使用的是 4.1).我应该注意,如果我将$subjectID"替换为使用硬编码的字符串 ID(例如5ff4c037bc0a716381231277"),一切都如您所愿.任何人都可以帮助我完成我的意图吗?由于此脚本仅打算执行一次,因此性能问题不大.

everything works except for one thing: when I try to pass ObjectId("$subjectID") as a parameter to "get_location_doc", it parses "$subjectID" as a literal string rather than passing the value of the underlying field in each document. I have also tried passing simply subjectID (without quotes) in which case it was simply undefined, or "$$subjectID" which led me to a literal string again. I understand this is due to client/server side parsing in run time. I have tried to utilize the "$function" operator, but apparently it's only available from version 4.4 (I'm using 4.1). I should note, that if I replace "$subjectID" with a hard-coded string ID (for example "5ff4c037bc0a716381231277") everything works as you'd expect. Can anyone please help me accomplish what I intend? since this script is only meant to be executed once, performance is not much of an issue.

谢谢!

推荐答案

db.getSiblingDB().collection.find() 是一个客户端操作.它不是您可以用来将集合作为查询的一部分加入的东西.为此,请参阅 https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/.

db.getSiblingDB().collection.find() is a client-side operation. It is not something you can use to join collections as part of a query. For that, see https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/.

您要做的第二件事是从文档中检索嵌套字段.你可以用 $set 和点符号来做到这一点.具体参见 中的示例https://docs.mongodb.com/manual/reference/operator/aggregation/set/#adding-fields-to-an-embedded-document.

The second thing you are doing is retrieving nested fields out of a document. You can do this with $set and dot notation. See specifically the example at https://docs.mongodb.com/manual/reference/operator/aggregation/set/#adding-fields-to-an-embedded-document.

您将需要构建一个单一的聚合管道,该管道仅使用 https://docs.mongodb.com/manual/reference/operator/aggregation/https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/.

You will need to construct a single aggregation pipeline that does everything your current mix of aggregation and javascript does using only the operations documented in https://docs.mongodb.com/manual/reference/operator/aggregation/ and the stages documented in https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/.

这篇关于从不同的数据库更新集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆