- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 MongoDB/Node 后端中,我有一个函数,可以根据传入的订阅 _ids 更新标题为 subscriptionEnd
的字段上的日期。它看起来像这样:
try {
db.collection('clients').updateMany(
{ "subscription._id": { $in: mongoArrSubscription } },
{ $set : {"subscription.$.subscriptionEnd": lastDayOfMonth } },
function (err, res) {
if (err) throw err;
});
res.sendStatus(200);
} catch (e) {
console.log(e);
}
}
这按原样工作。然而,照原样,这只会针对“订阅”数组中的第一个元素。我现在意识到我们有实例,同一个数组中有两个元素可以传入。所以,我的想法是在这个函数中使用 $unwind
,首先 $unwind “订阅”数组。然而,我对语法有点不清楚,以及这是否可行。
我的展开聚合如下所示:
Client.aggregate( [ { $unwind : "$subscription" } ] );
有没有办法可以将这个 $unwind
聚合链接在一起,以便它发生在我的 try/catch block 中的 updateMany()
之前?该语法是什么样的?我是否将这些操作链接在一起,或者将 $unwind
作为参数传递给 updateMany()
?
我尝试了这个,将 $unwind 操作作为第一个参数传递:
try {
db.collection('clients').updateMany( { $unwind : "$subscription" },
{ "subscription._id": { $in: mongoArrSubscription } },
{ $set : {"subscription.$.subscriptionEnd": lastDayOfMonth } },
function (err, res) {
if (err) throw err;
});
res.sendStatus(200);
} catch (e) {
console.log(e);
}
}
...但它出错了:
MongoError: Unknown modifier: $unwind
所以也许我需要先进行聚合 $unwind,作为单独的操作,然后运行 updateMany()
?
更新:有人指出 $unwind
不能与 updateMany()
一起使用,所以也许我需要执行 $unwind
操作首先单独运行,然后运行我的 updateMany()
操作?
最佳答案
您可以使用较低版本的聚合和批量写入来模拟 3.6 多重更新。
类似的东西
var bulk = db.getCollection('clients').initializeUnorderedBulkOp();
var count = 0;
var batch = 1;
db.getCollection('clients').aggregate([
{$match:{"subscription._id":{$in:mongoArrSubscription}}},
{$unwind:"$subscription"},
{$match:{"subscription._id":{$in:mongoArrSubscription}}},
{$project: {_id:0, subscription_id:"$subscription._id"}}
]).forEach(function(doc){
var subscription_id = doc.subscription_id;
bulk.find({"subscription._id":subscription_id}).updateOne(
{$set:{"subscription.$.subscriptionEnd": lastDayOfMonth}}
);
count++;
if (count == batch) {
bulk.execute();
bulk = db.getCollection('clients').initializeUnorderedBulkOp();
count = 0;
}
});
if (count > 0) {
bulk.execute();
}
关于javascript - 在 MongoDB 中的 updateMany() 之前使用 $unwind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49676942/
我有一个从 A View Controller 到 B View Controller 的 unwind segue。 在B中做了一个网络操作,操作完成后,响应会显示在A View Controlle
我刚刚意识到,在当前的 Xcode 8 beta 6 中,当我将展开操作连接到例如模态呈现的 View Controller 中的条形按钮项目以展开模态呈现转场并呈现 View Controller
我是 mongodb 聚合的新手。我的 mongo 文档有很多数组。我需要将其导出为平面文件。为此,我需要构建它。我尝试了以下聚合: [ {$unwind : "$items" }, {$unwind
我正在尝试使用 De Bruijn 指数定义 lambda 演算项。我在 OS X 上使用 swi prolog。 如果我使用自然数的 zero|successor 表示,我可以交互式地完成部分指定的
我是 mongodb 聚合的新手。我的 mongo 文档有很多数组。我需要将其导出为平面文件。为此,我需要构建它。我尝试了以下聚合: [ {$unwind : "$items" }, {$unwind
有了这些数据: { "_id" : ObjectId("576948b4999274493425c08a"), "virustotal" : { "scan_id" :
我想要$unwind 2 个字段,school 和home。数据库结构是这样的; { "id" : 1, "school" : [ { "pat
考虑这组测试结果: [{ _id: ObjectId(...), name: "Test1", acts: [ { name: "act1",
我想要 go 中的结果,因为 mongo shell 提供给我。 在 mongo shell 中数据是这样的: db.user.aggregate([{$unwind:"$user"}]).prett
这个问题 - Is it possible to get a slice of a slice in Mongo?涵盖了如何在 Mongo 中获取切片。简而言之,使用聚合链来$unwind、$skip
我有一个用户集合,其中每个文档具有以下结构: { "_id": "", "login": "xxx", "solved": [ { "problem": "",
在 MongoDB 聚合框架中,我希望在对象(即 JSON 集合)上使用 $unwind 运算符。看起来不像是 possible ,有解决方法吗?有计划实现吗? 例如,从聚合 documentatio
假设我有一个名为节点的参数: "nodes": [ { "name": "John", "age": 18.0, "label": "Person",
我的文档有一个字段symptoms,它是一个数组的数组。 我想$unwind它并应用$addToSet删除重复项, 然后只需更新文档即可。怎么做 ? 文档 "symptoms": [ [
我正在编写一个非常简单的应用程序,它有两个 View Controller - FirstVC 和 SecondVC。在 FirstVC 上,我加载了一个视频 (apples.mp4),它在后台(在我
我在想,在聚合管道中为嵌套数组的文档使用 $unwind 运算符是否会以与数组中项目的顺序相同的顺序返回解构的文档。例子:假设我有以下文件 { "_id" : 1, "item" : "foo", v
我有一份教育机构文件,看起来像这样: { name: ..., addresses: [...], courses: [ {name: ... , duration: ..., tags[...]}
使用 Storyboard这非常容易。您只需将操作拖至“退出”即可。但我应该如何从我的代码中调用它? 最佳答案 创建手动转场(ctrl-从文件所有者拖动到退出), 在绿色 EXIT 按钮下方的左侧 C
iOS 6 和 Xcode 4.5 有一个称为“Unwind Segue”的新功能: Unwind segues can allow transitioning to existing instanc
我有一个 Storyboard,它有一个起始 View Controller ,它呈现其他模态视图 Controller 。 假设 View Controller A 是起始 View Control
我是一名优秀的程序员,十分优秀!