These are chat archives for Automattic/mongoose

7th
Nov 2016
yaragallamurali
@yaragallamurali
Nov 07 2016 00:50
hi any one there
Paul "Joey" Clark
@joeytwiddle
Nov 07 2016 03:31
@yaragallamurali When you paste code, please put ``` before and after it.
Perhaps the problem is that you created your query but you didn’t actually execute it yet!
You can use .exec() but I prefer to use promises with .then()
obsr.then(function (results) {
    console.log(“obsr got results:”, results);
}).catch(console.error);
Jelani Thompson
@JelaniThompson
Nov 07 2016 03:39

(X-Post from MongoDB)

Hey there! I'm new to MongoDB and working with Mongoose currently. In the Mongoose documentation it specifies that you can create a schema and then convert it to a model

However, I'm trying to assign a variable name to my constructor based on the input from my form

So I have this

https://gist.github.com/JelaniThompson/9460da845900657ef969450e5eca44de

How can I assign the new User constructor a variable name without it being predefined?

Pretty much want the value of the username from my input field in a form to be the name of the constructor

Listu
@Listu
Nov 07 2016 09:11
Hi there. How can I findAndModify multiple documents ? I have Users in which I put Notification (it the same notification) and I would like to remove it from all users I put it. How can I do that ?
Paul "Joey" Clark
@joeytwiddle
Nov 07 2016 09:13
Your code looks fine to me @JelaniThompson
@Listu Hard to say without seeing your data. Perhaps you want $unset
Listu
@Listu
Nov 07 2016 10:12

@joeytwiddle

UserSchema : {
  _id: String,
  notifications: [Object]
}

Notification: {
  id: uuid
}

I pushed Notification to multiple users. When a user handles a notification, I would like to delete the notification in all users which have this notification. To remove from a user,

'$pull': { 'notifications.$.id': id}

must be good, but the problem is to modify multiple users at the same time.

I could find all users then loop on them, remove the notification and save one by one, but if I can do it with one request, it will be better ^^
Paul "Joey" Clark
@joeytwiddle
Nov 07 2016 10:15
User.update(query, update)
should do multiple documents.
You can test your query with a find first. When it is working, put the $pull into update.
Listu
@Listu
Nov 07 2016 10:18
Can I get the users in the callback with update ? I tried it but get something weird for me ^^
{ ok: 1,
  nModified: 0,
  n: 0,
  opTime:
   { ts: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1478513797 },
     t: 0 },
  electionId: 7fffffff0000000000000000 }
Paul "Joey" Clark
@joeytwiddle
Nov 07 2016 10:19
Oh. According to the docs, need to pass an option {multi:true}
http://mongoosejs.com/docs/api.html#model_Model.update
Listu
@Listu
Nov 07 2016 10:19
Yes I did it already ^^
Paul "Joey" Clark
@joeytwiddle
Nov 07 2016 10:19
I don’t see any way to get the documents back.
nModified will tell you how many documents were changed.
Listu
@Listu
Nov 07 2016 10:21
I guess. Oh maybe if there is at least one modified it will send the user.
Paul "Joey" Clark
@joeytwiddle
Nov 07 2016 10:22
I fear not. If you really need the list of documents, you may want to execute a find first.
This message was deleted
Listu
@Listu
Nov 07 2016 10:25
Ok :/
Listu
@Listu
Nov 07 2016 10:40

Ok last question :) is it better to find just by the notification.id or by user._id and notification.id ? I mean if I do something like

User.find({
  '_id' : { '$in': [IDs]},
  'notifications.id': data.notification.id
}...

maybe it will find the users matched by IDs, then filter them by notifications.id ? What could reduce the time of the request I guess

Tseno Tsenov
@EducatedMF
Nov 07 2016 14:24
Hi! I am searching my collection for documents, but when searching for a non existant ID it throws an error with an empty object {}
Is this standard behaviour or am i doing something wrong?
I am using promises and .exec()
Tionna Wilson-Pierce
@createfortionna_twitter
Nov 07 2016 17:49
Hello All! I am trying to delete data from my database using mongoose. I retrieved my data using a jQuery ajax call. Any help?? :) Gladly appreciate it
Jelani Thompson
@JelaniThompson
Nov 07 2016 18:22
Hey there @joeytwiddle Yeah the code I posted is functional but it was just a question on how I should go about implementing something