Skip to content Skip to sidebar Skip to footer

How To Return Subdocuments From An Object In An Array With Meteor And MongoDB

This is a followup question from my previous question here on stackoverflow.com. Firstly, Thanks to @David Weldon: I was able to structure my update properly. However when I go to

Solution 1:

!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.


Answer :

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

Like So:

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?

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.

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 stackoverflow.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.

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

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
}

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.


What I Learned:

  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

Thank you:

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


Important Links:

  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)

Related Question:

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


Post a Comment for "How To Return Subdocuments From An Object In An Array With Meteor And MongoDB"