0

So, a little background about what I'm trying to create. I am trying to build a voting app, where a MongoDb database stores the options and the counters for each of those options to check how many times people have voted for each option. The data structure I have implemented looks like below.

_id:"57dfecfa3832360f46e183c2"
options:"iea, aoegboae, aoeugoa, ougr, gege"
options_counter:Object
0:0
1:0
2:0
3:0
4:0
__proto__:Object
title:"hr"

Each of those numbers [0,1,2,3,4] represents an option and the number after the colon represents the counter for each. My function to update the required counter is shown below

   var fieldToIncrement = db.collection('polls').options_counter.field;
        db.collection('polls').findOneAndUpdate(
            {_id: user_id},
            {$inc: {fieldToIncrement: 1}}, 
            {upsert:false}, 
            {returnNewDocument: true}
        ).toArray(function(err, doc){
           if(err){
               throw new Error(err);
           }if(doc){
               console.log(doc);
               //callback(null, doc);
           }
        });

However, using the fieldToIncrement variable returns an error saying that cannot find property field of undefined. The variable field contains the number specifying the specific key of the options_counter object that needs to be incremented by 1. I have embedded the options_counter within the original document. My question is how do I set up the syntax so that MongoDb can find the specific key value pair within the options_counter object to update, and increment that counter by 1?

2
  • There are a couple problems, but start with calling console.log(fieldToIncrement) to show its value. It's likely not what you think it is. Commented Sep 19, 2016 at 14:21
  • So when I use the above syntax to define fieldToIncrement, it returns an error because I am apparently trying to access a property of an undefined object. I tried changing it to just options_counter.field, but that returns an error as well because options_counter can't be accessed outside the database. What are the other errors you see? Commented Sep 19, 2016 at 14:24

1 Answer 1

1

It looks like "options_counter" is a embedded document, for this you var fieldToIncrement has to be something like options_count.0. I can't see this in your question.

please share your code where you define fieldToIncrement, is now seems undefined as also stated in the error you mentioned: "However, using the fieldToIncrement variable returns an error saying that cannot find property field of undefined."

Sign up to request clarification or add additional context in comments.

5 Comments

I defined it in the very first line of the second block of code. var fieldToIncrement = db.collection('polls').options_counter.field; I tried your suggested method, but that returns an error as well since options_counter can only be accessed inside the db.collection functions
You must change it to something like: var fieldToIncrement = { $inc :{ options_counter.0 :1}}. Use that var in your update instead of the {inc .....} line.
So, I did that. But, it doesn't seem to have had any effect on the document. The document does get returned, but with none of the effects of the update seen.
@ZaidHumayun I don't know the layout of your model so i guessed the options_counter.0 for 0 use the var in the embedded document. If it's not clear let me know we'll start a discussion
Hey, I figured out what was wrong. It had less to do with an error in my code and more to do with the haphazard documentation of MongoDb on how to return the updated document. Thanks for your help btw!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.