accessing nested objects in mongo using node.js - node.js

I'm unable to access a nested object in a mongo doc. None of the online solutions or even mongo documentation has been able to resolve my problem.
{
characterName:character.characterName,
0:{slotName:"", slotCount:0},
1:{slotName:"", slotCount:0},
2:{slotName:"", slotCount:0},
3:{slotName:"", slotCount:0}
}
I want to access any of the numbered keys via an api and change the value of slotCount.
I've tried everything I could find online including the below.
db.Inventories.updateOne(
{characterName:user.selectedCharacter.CharacterName},
{$set:{[req.query.slotNumber].0.slotCount:req.query.itemCount}}
);
I'm getting syntax errors from VSCode as it's not expecting a period . after the first key on the last line of code.
SOLUTION: create a var that stores a string
key =`${req.query.slotNumber}.slotCount`
and then use square brackets when calling the key
db.Inventories.updateOne(
{characterName:user.selectedCharacter.CharacterName},
{$inc:{[key]:-1}}

Related

Problem accesing a dictionary value on dialogflow fullfilment

I am using fullfilment section on dialogflow on a fairly basic program I have started to show myself I can do a bigger project on dialogflow.
I have an object setup that is a dictionary.
I can make the keys a constant through
const KEYS=Object.keys(overflow);
I am going through the values using
if(KEYS.length>0){
var dictionary = overflow[keys[i]]
if I stringify dictionary using
JSON.stringify(item);
I get:
{"stack":"overflow","stack2":"overflowtoo", "stack3":3}
This leads me to believe I am actually facing a dictionary, hence the name of the variable.
I am accesing a string variable such as stack and unlike stack3
Everything I have read online tells me
dictionary.stack
Should work since
JSON.stringify(item);
Shows me:
{"stack":"overflow","stack2":"overflowtoo","stack3":3}
Whenever I:
Try to add the variable to the response string.
Append it to a string using output+=${item.tipo};
I get an error that the function crashed. I can replace it with the stringify line and it works and it gives me the JSON provided so the issue isnt there
Dictionary values are created as such before being accessed on another function:
dictionary[request.body.responseId]={
"stack":"overflow",
"stack2":"overflowtoo",
"stack3":3 };
Based on the suggestion here I saw that the properties where being accessed properly but their type was undefined. Going over things repeatedly I saw that the properties where defined as list rather than single values.
Dot notation works when they stop being lists.
Thanks for guiding me towards the problem.

Key not being recognized in JSON

I have following JSON that i am trying to parse.
{"id":1,"colour":"blue","count(colour)":1}
It is a result of what i have returned from my sqlite3 select staement. I am doing a count(colours), which is being returned as the key in the JSON.
Then when i attempt to reference the value using .count(colour), my node app fails giving me an error that colour is not defined. Note, that referencing .id works just fine.
Has anybody ran into this issue before or can provide any help?
If your JSON is in some variable, myjson, and you access it directly with myjson.count(colours), you get the error because it's trying to execute a function in your object.
It works as you expect if you access via string like this: myjson["count(colour)"].

group by in Flask-MongoAlchemy

i stared new project with flask web-framework with mongoDB. i also used database and access data using Flask-MongoAlchemy. i tried work on different query like .all(), .filter_by(), .get() its work nice. but problem is that how to use aggregate funcation in Flask-MongoAlchemy ? for example i want to use group_by.
i tried following but its still not working.
db.User.qroup_by(name)
its gives following error
'BaseQuery' object has no attribute 'group_by'

Resource Conflict after syncing with PouchDB

I am new to CouchDB / PouchDB and until now I somehow could manage the start of it all. I am using the couchdb-python library to send initial values to my CouchDB before I start the development of the actual application. Here I have one database with templates of the data I want to include and the actual database of all the data I will use in the application.
couch = couchdb.Server()
templates = couch['templates']
couch.delete('data')
data = couch.create('data')
In Python I have a loop in which I send one value after another to CouchDB:
value = templates['Template01']
value.update({ '_id' : 'Some ID' })
value.update({'Other Attribute': 'Some Value'})
...
data.save(value)
It was working fine the whole time, I needed to run this several times as my data had to be adjusted. After I was satisfied with the results I started to create my application in Javascript. Now I synced PouchDB with the data database and it was also working. However, I found out that I needed to change something in the Python code, so I ran the first python script again, but now I get this error:
couchdb.http.ResourceConflict: (u'conflict', u'Document update conflict.')
I tried to destroy() the pouchDB database data and delete the CouchDB database as well. But I still get this error at this part of the code:
data.save(value)
What I also don't understand is, that a few values are actually passed to the database before this error comes. So some values are saved() into the db.
I read it has something to do with the _rev values of the documents, but I cannot get an answer. Hope someone can help here.

Array to function parameters

I'm using the node.js Redis library and I'm attempting to bulk-subscribe to many keys. I've got an array which is dynamic i.e
var keys {'key1','key2',...,'keyN'}
and I want to feed each index in as parameters to subscribe in the Redis library which takes one or more string(s). I've tried the apply function in JS using..
redisClient.subscribe.apply(this,keys);
but it doesn't cause a subscription. Any suggestions on how I can get over this issue?
Your example data is totally invalid JS, but I'm assuming you have it correct in your code.
You need to set the proper function context:
redisClient.subscribe.apply(redisClient, keys);

Resources