找到你要的答案

Q:Adding to a non-existent array in a collection with $addToSet

Q:添加一个不存在的数组集合中的addtoset美元

This should be very simple. I have two collections, one of which holds two types of data (name, age), and the other should simply add the age values to an array (with no duplicates).

I "start" my collections like usual:

People = new Mongo.Collection('people')
Ages = new Mongo.Collection('ages')

Right now I'm working with seed data, but the question could easily extend to when I actually want to dynamically add data to the array. I seed it like so:

Meteor.startup(function() {

  if (People.find().count() === 0) {
    [
      {
        name: 'John',
        age: '24' //Yes, I want to store it as strings.
      },
      { ... } //more data
    ]
    .forEach(function(person) {
      People.insert(person)
      Ages.update({ $addToSet: {age: person.age}}) //Not working
    })
  }
})

That last part there is what's not working. I guess I figured $addToSet would fix things for me, since the docs say:

If the field is absent in the document to update, $addToSet creates the array field with the specified value as its element.

Now I suppose I have to create the field first, but I'm not sure where or how. I have a strong, strong feeling that I'm overlooking something ridiculously simple here...

这应该很简单。我有两个集合,其中一个包含两种类型的数据(名称、年龄),另一种只需将年龄值添加到数组(没有重复)。

我像往常一样“开始”我的收藏:

People = new Mongo.Collection('people')
Ages = new Mongo.Collection('ages')

现在我正在处理种子数据,但当我想动态向数组添加数据时,这个问题很容易扩展到。我像这样播种:

Meteor.startup(function() {

  if (People.find().count() === 0) {
    [
      {
        name: 'John',
        age: '24' //Yes, I want to store it as strings.
      },
      { ... } //more data
    ]
    .forEach(function(person) {
      People.insert(person)
      Ages.update({ $addToSet: {age: person.age}}) //Not working
    })
  }
})

最后一部分有什么不工作。我想我addtoset美元会修理东西的我,因为医生说:

If the field is absent in the document to update, $addToSet creates the array field with the specified value as its element.

现在我想我必须先创建字段,但我不知道在哪里或如何。我有一种强烈的、强烈的感觉,我俯视那些可笑的简单的在这里…

answer1: 回答1:

If I got it right, your db should look like that when filled

Persons (_id, name, age)

1, John, 24
2, Pete, 21
3, Michele, 27
4, Sandy, 21

Ages (_id, ageset)

?, [ 24, 21, 27 ]

Solution1: Just insert one record on a fix key and then only update this one.

Have a look at this MeteorPad

Solution2: Using a local Meteor.Collection which is synced by server an gets DISTINCT field values from package mrt:mongodb-aggregation.

Have a look at this MeteorPad

Solution3: Using a server side synced Mongo.Collection to hold the distinct ages list.

Have a look at this MeteorPad

Remark: Checkout log infos on server process. There are timeouts to add, change and remove a record for test and updates (5 sec, 10 sec, 15 sec)

如果我得到它的权利,你的DB应该看起来像填充时

人(_id,姓名,年龄)

1, John, 24
2, Pete, 21
3, Michele, 27
4, Sandy, 21

年龄(_id,ageset)

?[ 24,21,27 ]

解决方法:只是插入一个记录在一个固定的关键,只有更新这一。

在这meteorpad一看

解决方案:使用本地的流星。收藏是同步的服务器获取不同的字段值从包装捷运:MongoDB聚集。

在这meteorpad一看

3:使用服务器端同步Mongo。收藏着不同年龄的列表。

在这meteorpad一看

备注:检查日志信息在服务器进程。有超时的添加,更改和删除测试和更新记录(5秒,10秒,15秒)

answer2: 回答2:

Now right now, I see that you're defining your People collection, but I don't see you actually defining "person" or "Age" anywhere. Maybe thats just due to how you've formatted your answer.

Either way though, I'm not entirely sure you'd be getting anything to happen. As far as I know, you'll need to select the documents each time through the loop, as you want to update them.

This is how I'm doing something similar in an app I'm working on:

Meteor.users.update({ _id: Meteor.userId() }, { $addToSet: { 'profile.viewedRequests' : this._id }});

The key there being that I'm selecting an individual document, before attempting to update it.

Its either that, or you need to switch to People.update.

现在,我看到你正在定义你的人集合,但我不认为你实际上定义“人”或“年龄”任何地方。也许这只是由于你如何格式化你的答案。

无论哪种方式,虽然,我不完全肯定你会得到任何事情发生。据我所知,你需要每次通过循环选择文档,因为你要更新它们。

这就是我在做一个类似的应用程序的工作:

Meteor.users.update({ _id: Meteor.userId() }, { $addToSet: { 'profile.viewedRequests' : this._id }});

关键是,我正在选择一个单独的文档,然后尝试更新它。

要么,或者你需要切换到people.update。

mongodb  meteor