1

Given the following schema

{ 
  "_id" : ObjectId("55e0ecfa422d86f0a3b37b90"), 
  "username" : "Michael", 
  "hostedEvents" : [ 
    { "_id" : "1", "usersApplied" : [ ] } 
  ],
  "joinedEvents" : [ 
    { "confirmed" : false } 
  ] 
}

And I am trying to push into the usersApplied array the string "Lisa"

Right now I have a mongodb command that works but I want to do this in mongoose:

db.users.update({username:'Michael', "hostedEvents._id": "1"},
             {$addToSet: {"hostedEvents.$.usersApplied": {"username":"Lisa"}}});

I tried to do it in mongoose this way:

Users.update({username:'Michael', "hostedEvents._id": "1"},
{$addToSet: {"hostedEvents.$.usersApplied": {"username":"Lisa"}}});

I am trying to follow the recommendation made here

Community
  • 1
  • 1
MichaelGofron
  • 1,310
  • 4
  • 15
  • 29
  • So what is the problem? Aside from that neither of these actions are attempting to *"push the string Lisa" (sic)*? – Blakes Seven Aug 29 '15 at 23:08
  • The question has been updated to use "Lisa" now. But the real question I have is how to convert the mongodb command to mongoose. My mongoose command does not work but my mongodb command does. The mongoose command does not throw an error it simply doesn't push the string "Lisa" – MichaelGofron Aug 29 '15 at 23:12
  • What do you mean "convert"? There is no conversion required, as it just works the same. The only thing missing is the callback definition or whether you want to return a promise. If you are getting errors or some other unexpected result ( again not mentioned in your question what problems you are having ) then it will likely be a cause of your schema definition being incorrect. Please actually state your problem and possibly post your schema. – Blakes Seven Aug 29 '15 at 23:40
  • That's what I thought, that the schema would work exactly the same way as the mongodb query and I would simply change db.users to be Users. However this did not update my database like I expected, so I was wondering if there was some other proper way to do the mongodb command in mongoose or if I were missing something – MichaelGofron Aug 29 '15 at 23:41
  • Is there a problem or not? This really is not the place to be asking "does my code look right?". There is another site for that :[codereview.stackexchange.com](http://codereview.stackexchange.com/) – Blakes Seven Aug 29 '15 at 23:43
  • Can you explain please what 'hostedEvents.$.usersApplied' does? – Vandervidi Nov 20 '15 at 22:49

1 Answers1

0

I think you should use $push instead

    Users.update({username:'Michael', "hostedEvents._id": "1"},
{$push: {"hostedEvents.$.usersApplied": {"username":"Lisa"}}});

That should work.