简体   繁体   English

MongoDB:$推送多个对象,并在单个更新中弹出多个对象

[英]MongoDB: $push Multiple Objects, and $pop Multiple Objects in Single Update

Imagine a MongoDB document with an array of 100 objects. 想象一下包含100个对象的MongoDB文档。 And we want to keep the array length fixed at 100 . 我们希望将数组长度固定100 When a batch of new objects arrives (could be 1, 5, 10, etc.), we want to update the array with new objects, while removing an equal amount of old objects so the array length will stay fixed. 当一批新对象到达时(可能是1,5,10等),我们希望用新对象更新数组,同时删除等量的旧对象,这样数组长度将保持不变。 I've opted to reading the array from MongoDB into my app, making some modifications, then using $set to update the array: 我选择将MongoDB中的数组读入我的应用程序,进行一些修改,然后使用$set更新数组:

var newData = [
  { ... },
  { ... },
  { ... },
  { ... },
  { ... }
];
var oldData = Collection.findOne({exchange: 'The Exchange', market: 'The Market'}).data;
newData = newData.concat(oldData).slice(0, 100);
Collection.update(
  {
    exchange: 'The Exchange',
    market: 'The Market'
  },
  {
    $set: {
      data: newData
    }
  }

Is it possible in MongoDB to $push the new on to the front, while simultaneously using $pop to remove an equal amount of objects off the back? 是否有可能在MongoDB中将$push到前面,同时使用$pop从后面移除相同数量的对象?

Is it possible in MongoDB to $push the new on to the front, while simultaneously using $pop to remove an equal amount of objects off the back? 是否有可能在MongoDB中将$推到前面,同时使用$ pop从后面移除相同数量的对象?

Yes, using $slice : 是的,使用$slice

$push: {
    data: {
       $each: [ { ... }, { ... } ], // your batch
       $slice: -100 // maximum array size
    }
 }

For an example, see http://docs.mongodb.org/manual/tutorial/limit-number-of-elements-in-updated-array/ 有关示例,请参阅http://docs.mongodb.org/manual/tutorial/limit-number-of-elements-in-updated-array/

Well the answer is both "yes" and "no" to put it in slightly confusing terms. 那么答案是“是”和“否”,将其置于略微混乱的条款中。 What you cannot do is both $push and $pull operations on the same array in a singular update. 你不能做的是在单一更新中同一阵列上的$push$pull操作。 This is not allowed for the "same path" of operations because neither $push or $pull is really determined to occur in any order within the current update syntax. 对于“相同路径”的操作不允许这样做,因为$push$pull实际上确定不会以当前更新语法中的任何顺序发生。

However in your specific context, that is not what you are asking. 但是,在您的具体情况下,这不是您所要求的。 To do what you want, MongoDB supports the $slice modifier which can be used along with $each . 为了做你想做的事,MongoDB支持$slice修饰符,可以和$each一起使用。 This will effectively "limit" the total size of the array as new items are added to it. 当新项目添加到阵列时,这将有效地“限制”阵列的总大小。

Following your example: 按照你的例子:

Collection.update(
    { "exchange": "The Exchange", "market": "The Market" },
    { "$push": { "data": { "$each": newData, "$slice": 100 } } }
)

That effectively limits the size of the array to the first 100 members whilst adding the new items to it. 这有效地将阵列的大小限制为前100成员,同时向其添加新项目。

Take note though that this may not be supported in the client version of "minimongo" as implemented by meteor. 请注意,meteor实现的“minimongo”客户端版本可能不支持此功能。 What you can do though is execute this on the server, and expose the method via publish. 你可以做的是在服务器上执行它,并通过发布公开方法。 There are plenty of examples to show you how to use publish. 有很多示例可以向您展示如何使用发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM