如何使用Meteor和MongoDB从数组中的对象返回子文档

如何使用Meteor和MongoDB从数组中的对象返回子文档

问题描述:

这是我先前的问题(在此处*.com .

This is a followup question from my previous question here on *.com.

首先,感谢 @David Weldon :我能够正确地组织更新.但是,当我从数据库获取输出时,我添加到score的对象将丢失.

Firstly, Thanks to @David Weldon: I was able to structure my update properly. However when I go to get the output from the database the object I added to score seams to be missing.

也许这是db.ideas.find()的默认行为.我希望在执行db.ideas.find()时也会得到子文档.

Maybe this is the default behavior of db.ideas.find(). I expected to also get the subdocuments when I performed the db.ideas.find().

我运行的代码:

Ideas.update("bKXXrpYmppFBfq9Kx", {
  $addToSet: {
    score: { userId: "W9YEs84QFhZzJeB6j", score: 1 },
    votedOnBy: "W9YEs84QFhZzJeB6j"
  },
  $inc: {
    overallScore: 1,
    timesVotedOn: 1
  }
});

mongo数据库控制台的输出:

Output from mongo db console:

meteor:PRIMARY> db.ideas.find()
{
    "_id" : "bKXXrpYmppFBfq9Kx",
    "title" : "Jump through the portal",
    "body" : "The elves from Eldernland should jump through the blue portal mentioned in episode one.",
    "userId" : "W9YEs84QFhZzJeB6j",
    "author" : "Nate Beck",
    "episodeId" : "LbDynnAHxAgM5PPXM",
    "timestamp" : ISODate("2016-06-07T20:37:05.775Z"),
    "votedOnBy" : [
        "W9YEs84QFhZzJeB6j"
    ],
    "timesVotedOn" : 3,
    "score" : [
        {

        }
    ],
    "overallScore" : 1
}

我希望看到分数"中的内容看起来像是一个空对象.

I was expecting to see what was in "Score" this looks like an empty object.

有什么想法在这里出什么问题吗?

Any ideas what is going on wrong here?

!已解决!

这里的问题是我不知道 aldeed:collection2 包在做什么我的代码.我添加了此程序包以使用 OrionJS (这是 aldeed:collection2 迫使我对所有更新进行了验证.我以前曾遇到过这个问题,但它给了我一个错误.过去,我能够找到错误所在.这次,任何地方没有错误.它将更新数组,但对象为空.太混乱了.我将在 aldeed:collection2 的项目页面上发布一个问题.

!SOLVED!

The problem here is that I was unaware what the package aldeed:collection2 was doing to my code. I added this package to use OrionJS (here's the github page). And I didn't notice that this aldeed:collection2 was forcing validation on all of my updates. I was running into this before, but it was giving me an error. In the past I was able to trace the error down. This time, there was no error anywhere. It would update the array, but with an empty object. So confusing. I'm going to post an issue on aldeed:collection2's project page.

项目文档中:"[

From the project documentation: "[aldeed:collection2 is a] Meteor package that allows you to attach a schema to a Mongo.Collection. Automatically validates against that schema when inserting and updating from client or server code."

用于修正的文档可以是在此处找到.

The documentation for the correction can be found here.

因为我正在使用 aldeed:simple-schema aldeed:collection2 我需要确保附加正确的"Schema "添加到我的收藏夹中.

Because I'm using aldeed:simple-schema and aldeed:collection2 I need to make sure to attach a proper "Schema" to my collection.

像这样:

Ideas.attachSchema(new SimpleSchema({
  // ... a bunch of other schema data

  // I was missing this:
  score: {
    type: [Object],
    optional: true,
    label: 'Score',
  },
  "score.$.userId": {
    type: String,
    optional: true,
    label: 'Score'
  },
  "score.$.score": {
    type: String,
    optional: true,
    label: 'Score'
  }

  // ... a bunch more schema data
});


故障排除:

我是怎么知道的?


Troubleshooting:

How did I figure this out?

好吧,我是MongoDB的新手,所以我不愿意转到控制台并尝试从那里进行更新.实际上,直到我与妻子交谈时,我什至没有想到.

Well, I'm new to MongoDB - so I was reluctant to go to the console and just try to do the update from there. In fact, this didn't even occur to me until I was talking with my wife.

我尝试了一切,在 一切 周围加了引号,并查看了其他人的代码.我检查了MongoDB的所有出色文档.我看了YouTube影片.我查看了其他*.com解决方案.最终我想到,其他人的代码看起来都和我的代码完全一样,这应该可以正常工作.我什至在 Clarity.fm 上签名,向

I tried everything, adding quotes around everything, looking at other people's code. I checked out all of MongoDB's excellent documentation. I looked at youtube videos. I looked at other *.com solutions. Finally it occurred to me that everyone else's code looks exactly like my code, and this should just WORK. I even signed up on Clarity.fm to ask a question from Sacha Greif for $1 a minute.

...我开始认为我的mongo数据库无法正常工作.也许 我的mongo 安装被破坏了?

... I was beginning to think that my mongo database wasn't working. Perhaps my mongo install was broken?

好吧,有一个测试方法……经过两天的折磨,它终于来到了我-只需在mongo控制台中尝试更新即可.(duh)

So, okay, there is a test for that... After two days of this torture it finally came to me - Just try the update in the mongo console... (duh)

meteor:PRIMARY> db.ideas.update({_id:"DqEGjK3xSTBdpEgXa"}, {$addToSet: {score: {userId: "123456", score: 1}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
meteor:PRIMARY> db.ideas.find()
{
  "_id" : "DqEGjK3xSTBdpEgXa",
  "title" : "Revive Randolf With Bloodmagic",
  "body" : "Bring Randolf back from the dead using witche's bloodmagic.",
  "userId" : "Rz28ByKYM4Y8futFb",
  "author" : "Iryna Iglehart",
  "episodeId" : "iQaxyLPi5iaYtQngf",
  "timestamp" : ISODate("2016-06-08T17:37:57.237Z"),
  "score" : [
    {
      "userId" : "123456",
      "score" : 1
    }
  ],
  "overallScore" : 0,
  "votedOnBy" : [ ],
  "timesVotedOn" : 0
}

一旦我知道mongo可以进行更新-我 知道 ,问题就出在我的流星设置上.如果这是个问题,到现在我会在流星上发现一个问题.我搜索了Google的每个暗角,试图弄清楚这一点.

Once I knew that mongo COULD do my update - I knew the problem had to be with my meteor setup. I would have found a issue on meteor by now if this was a problem. I searched every dark corner of google trying to figure this one out.

没多久,我就想起以前遇到过奇怪验证问题.除了这些验证问题之外,总是会出现错误.没有错误,并且只用空对象更新了数组,这一事实真的使我陷入了循环.

It wasn't long before I remembered I was having weird validation issues before. Except these validation issues always came with an error. The fact that there was no error and the array was simply updated with empty objects really threw me for a loop.

  1. 如果最初的流星失败,请尝试使用mongo控制台...
  2. 按照 @David Weldon 的评论-另一种对我有帮助的调试技术很久以前可能尝试过的方法是开始一个新的流星项目并在那里测试我的代码,然后一次添加一个软件包,并查看何时/是否破坏了代码.该测试将指出,从根本上讲,该代码是正确的,并且由于添加了程序包,因此仅需要附加的架构.
  3. 不要盲目添加软件包.了解软件包是否/何时更改了开发流程,例如 aldeed:collection2
  1. If at first meteor fails, try the mongo console...
  2. As per @David Weldon's comment - another good debugging technique which would have helped me, which I could have tried a long time ago is to start a fresh meteor project and test my code out there, then add packages one at a time and see when it/if any of them break the code. This test would have pointed out that fundamentally the code was correct, and only needed the attached schema because of an added package.
  3. Don't blindly add packages. Understand if/when packages change the flow of development such as aldeed:collection2


谢谢:

非常感谢 @Michel Floyd


Thank you:

A BIG THANKS to @Michel Floyd and @David Weldon for all your help on this problem.

  1. 我从常见错误中学到了很多东西: //*.com/users/635981/david-weldon">@David Weldon
  2. aldeed:collection2的文档
  3. 有关aldeed的文档:简单模式
  4. 此流星教程上搜索查找数据" -它讨论了fetch()及其有用之处.这在我对此问题进行故障排除期间为我提供了帮助.
  5. 在MongoDB中查看有关更新的文档
  6. MongoDB $ addToSet文档
  7. MongoDB $ push文档
  8. [MongoDB Bios示例集合]( https://docs. mongodb.com/manual/reference/bios-example-collection/] -正是在查看了此类文档之后,我才真正发现我的数据库设计没有问题,应该有一种方法一直以来(很明显)
  1. I learned quite a bit from common mistakes written by @David Weldon
  2. The documentation for aldeed:collection2
  3. The documentation for aldeed:simple-schema
  4. Search for "Finding Data" on this meteor tutorial - it talks about fetch() and how it is helpful. This helped me during my troubleshooting on this problem.
  5. Reviewing the documentation on about update from MongoDB
  6. MongoDB $addToSet Documentation
  7. MongoDB $push Documentation
  8. [MongoDB Bios Example Collection](https://docs.mongodb.com/manual/reference/bios-example-collection/] - It was after looking at documents like this that I really figured out there is nothing wrong with my database design, there SHOULD have been a way to do this all along (and there is, obviously)


相关问题:

[已解决]如何在MeteorJS中更新MongoDB集合上的子文档数组


Related Question:

[Solved] How to Update An Array of Subdocuments on a MongoDB Collection in MeteorJS