2

I have a rather complex MongoDB Operator that uses a where to look through multiple users to see if they have a certain value, and if so it changes the value of it. My problem is that the value it checks for can be different for different cases, so my $set would require me to add strings together to get the property that I would change. This is what I mean:

users.update({$where:function() {
  return this.profile.chatInfo.subscribedRooms.hasOwnProperty(findChatroom._id);
}},{$set: {"profile.chatInfo.subscribedRooms."+findChatroom._id:false}},{multi:true}
)

The only thing that does not work in that code is the part in the $set where I add "profile.chatInfo.subscribedRooms."+findChatroom._id

Another thing I have tried was making a variable that was equal to those strings added, and use the variable, but it also didn't work.

var addedString = "profile.chatInfo.subscribedRooms."+findChatroom._id;
users.update({$where:function() {
  return this.profile.chatInfo.subscribedRooms.hasOwnProperty(findChatroom._id);
}},{$set: {addedString:false}},{multi:true}
)

What I am actually trying to do here, is that whenever a chat message is sent, this operator is ran. It looks for every user that is subscribed to a room, and sets the value to false. The value being true or false just refers to whether or not the user has read the chat. This is to be used for notifications.

Edit: My question is not a duplicate of this because the MongoDB set command works differently from simply changing the value of an object's property. To add onto that, MongoDB can't even use the notation of object[property], because the docs say the only way to access embedded fields is through dot notation.

Community
  • 1
  • 1
Weastie
  • 157
  • 10
  • 1
    Possible duplicate of [How do I add a property to a JavaScript Object using a variable as the name?](http://stackoverflow.com/questions/695050/how-do-i-add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – Blakes Seven Mar 24 '16 at 02:09
  • Also `var query = {}; query[profile.chatInfo.subscribedRooms + findChatroom._id] = { "$exists": true }` is far better than using `$where`, in addition to setting the update statement by basically the same method. Not to mention that using "named keys" is horrible design. – Blakes Seven Mar 24 '16 at 02:12
  • Okay, thank you for the advice, I will look up how to use query because I don't even know how to use it. I edited the post to explain why it was not a duplicate. – Weastie Mar 24 '16 at 02:23
  • It's a duplicate. People worry about this too much. If the question title stands as a "reasonable" search term in itself ( and not the same as every other), then your question does not get deleted. It just places a "hold" that stops people from submitting answers that say exactly the same thing as the answers on the duplicate question. Just accept the duplicate. There is even some talk of giving a "little reward" for self accepting in the future. – Blakes Seven Mar 24 '16 at 02:32
  • But the question that you say my question is a duplicate of does not answer my question, I'm not trying to be stubborn, but I don't understand how I will get my question answered if I mark it as a duplicate but there are no other previous questions asked that answer my question. – Weastie Mar 24 '16 at 02:38
  • Also a duplicate of [Is there any way to use a dynamic key with node-mongodb-native?](http://stackoverflow.com/questions/7047209/is-there-any-way-to-use-a-dynamic-key-with-node-mongodb-native) – Blakes Seven Mar 24 '16 at 02:41
  • That won't solve my problem either, do you want me to repost with a better title? Am I even allowed to do that? – Weastie Mar 24 '16 at 02:45

1 Answers1

3

Actually those links that @BlakesSeven provided will solve your problem. It should be something like this,

var addedString = "profile.chatInfo.subscribedRooms."+findChatroom._id;
var $set = {};
$set[addedString] = false;

users.update({$where:function() {
    return this.profile.chatInfo.subscribedRooms.hasOwnProperty(findChatroom._id);
}}, { $set: $set }, { multi:true })
Kishor
  • 2,659
  • 4
  • 16
  • 34