0

How do I find course_id in meteor.users then insert/update/upsert my data into "classes" (an empty array)?

if cant find course_id then insert a new array, if i can then i update the array with new classes_id.

Empty classes array:

{
  "_id": "RoFFcaAfXBeR2napZ",
  "emails": [
    {
      "address": "tong@gmail.com",
      "verified": false
    }
  ],
  "classes": [],
  "courses": [
    "qwmZdgQbrZ3rmHdN8"
  ]
}

Insert new array to classes if i cant find course_id:

{
  "_id": "RoFFcaAfXBeR2napZ",
  "emails": [
    {
      "address": "tong@gmail.com",
      "verified": false
    }
  ],
  "classes": [
      {
         "course_id": "svesvinfdsgrvnekuktndvsk",
         "classes_id": ["myclass1"]
      },
  ],
  "courses": [
    "qwmZdgQbrZ3rmHdN8"
  ]
}

Add myclass2 to classes_id if i can find course_id

{
  "_id": "RoFFcaAfXBeR2napZ",
  "emails": [
    {
      "address": "tong@gmail.com",
      "verified": false
    }
  ],
  "classes": [
      {
         "course_id": "svesvinfdsgrvnekuktndvsk",
         "classes_id": ["myclass1", "myclass2"]
      },
  ],
  "courses": [
    "qwmZdgQbrZ3rmHdN8"
  ]
}
phongyewtong
  • 5,085
  • 13
  • 56
  • 81

1 Answers1

0

To find the record that has the course ID in question: Meteor.users.find({"courses": yourVariableHoldingTheId}) is an example query. This assumes that your publication has the courses field on it (more on publishing Users stackoverflow and Meteor.

From that query, you'll now have the Cursor of Users that meet your criteria. Iterate over the users and treat them as normal JS objects - check your criteria and either push to the classes subdocument, or push to the classes.$.classesId.

Pushing to classes Meteor.users.update({_id: "RoFFcaAfXBeR2napZ"}, {$push: {'classes': {'course_id': 'yourGibberishHere', 'classes_id': ['test']}}});

Pushing to the classes_id Meteor.users.update({"classes.course_id": "svesvinfdsgrvnekuktndvsk"}, {$push: {"classes.$.classes_id": "myclass3"}}); tested on Mongo 3.2, your mileage may vary in meteor (2.6 or 3.2, if you're using external mongo). Older commentary points to x.$.y not working in meteor/mongo $push, not sure if this is still the case.

If any of these fail, you can manipulate the object returned from the find and update it or subdocuments thereof using more accessible calls

Community
  • 1
  • 1
Greg Syme
  • 392
  • 3
  • 8
  • how do I check if the course_id exist? if exist push, if not exist create the course_id? – phongyewtong Feb 23 '16 at 07:36
  • I think i just findone, if nth then create course_id. dont know if that is the best way – phongyewtong Feb 23 '16 at 07:38
  • I suspect I may have address the incorrect course_id in my initial answer; please let me know if that's the case. To address checking for the course_id, (classes.$.course_id), you should be able to find as so `Meteor.users.find({"classes.course_id": "svesvinfdsgrvnekuktndvsk"});` or `Meteor.users.findOne({"classes.course_id": "svesvinfdsgrvnekuktndvsk"});` – Greg Syme Feb 23 '16 at 14:32