Mongoose not updating key value pairs stored with Object Schema - node.js

This is for rating feature in my application. I want to use the user-mail as key and the users rating as the value
Eg:
ratings : {
"user1#gmail.com" : 5,
"user2#gmail.com" : 4
}
I don't prefer using arrays since their could be just a single rating from each user.
I tried inserting a new key value pair in mongo using compass and it worked fine but when I did this using mongoose with type as Object in express, it is not working. Only the first key value pair is stored the user2's key value pair is not getting added.
Thanks in advance.

Schema type : Object
I have solved this using the method markModified("fieldname") before save().
Example:
mongooseSchema.markModified("ratings");
mongooseSchema.save();

Related

How to sort documents in MongoDB based on keys inside a dictionary in document using MongoEngine Python?

So I have documents of this type in my MongoDB Collection:
{
"_id" : ObjectId("606c66c875a2fe6153bfc71f"),
"t_id" : "12345678a",
"r_id" : "r12345678a",
"t_e_stats" : {
"acc" : "70"
},
"register_time" : ISODate("2021-04-06T19:18:56.890Z")
}
There are multiple of these documents with same 't_id' and different 't_e_stats' dictionary. But all 't_e_stats' dictionary have 'acc' key in them. Now I want to query the entry for a particular 't_id' with the maximum value of 'acc' in the 't_e_stats' dictionary. How do I do that in MongoEngine? Or I have found there are ways to run PyMongo queries in MongoEngine also, how to use that too if possible?
As commented by OP
Model.objects(t_id="12345678a").order_by("-t_e_stats.acc").first()
if anyone is interested in not only the object but also the JSON document, then you can do this:
[data_entry._data for data_entry in Model.objects(t_id="12345678a").order_by("-t_e_stats.acc")][0]
Use .sort
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html?highlight=find%20one#pymongo.collection.Collection.find_one
mycol.find_one({ "t_id" : "12345678a" }).sort("t_e_stats.acc", pymongo.DESCENDING)
This will return 1st record where t_e_stats.acc is max.
mongo shell
db.collection.find({ "t_id" : "12345678a" }).sort("t_e_stats.acc", -1).limit(1);
Mongoengine
Model.objects.get(t_id="12345678a").order_by("-t_e_stats.acc").first()

How to generate random ID instead of auto increment in Mongoose

I am creating an app where customers can scan codes, and when scanned, it opens a website which basically redeems that code. The problem is that all these codes have an auto-incremented ID, so the customer could just redeem all possible codes just by increasing the ID number in the url.. Is there a way to generate random uuid's in mongoose instead of the default auto incremented ObjectID?
You can achieve what you are looking for by the below command:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
where 4edd40c86762e0fb12000003 is the custom ObjectId you desire for.
To generate a completely random non-incrementing string, use the below code.
let randomString = _.times(16, () = (Math.random()*0xF<<0).toString(24)).join('');
var id = mongoose.Types.ObjectId(randomString);
Make sure that you are Inserting/Updating is a valid ObjectId on length 24 and a duplicate of the same ObjectId doesn't exist in another document in the same collection.
To generate a random ObjectId, use the below code.
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();
Note: A random ObjectId will be created if you don't pass any value to the parameter
Furthermore, you can insert any custom _id key inside MongoDB of any recognized MongoDB types, as long as its' unique in that collection, it will work.
Example:
db.col.insertMany([{"_id": 1}, {"_id": 2}])
db.col.insertMany([{"_id": "Product1"}, {"_id": "Product2"}])
will work just fine.
Instead of replacing the entire unique ObjectId with a random number, could you append a random number when printing out the code?
For example, parseInt(Math.floor(Math.random()*65536),16) will give you a random 4-character code. Store that as a validator when writing the document to the database, and append it to the ObjectId when generating the scannable token.
When the user scans the code, you can use the first 24 characters to look up the document, then verify that the last 4 characters match the validator. No external libraries required.
If you want to get even fancier, append a check digit or checksum on the code (such as is done with credit card numbers) so that the client side can sometimes determine that a code is invalid without even consulting the database.

How could I sort a svelte array-store?

I am doing a Todo app with store.
All is good now , but i want to sort the items in the store , or maybe to show them ordered.
The store is an array , and all item in the store is an object with the keys : text,id,editing,line,checkboxed.
So "checkboxed" is a boolean value , and i want that all the "checkboxed:true" objects would be first in the array.
how could i do it ?
Or maybe :
how could i sort items in svelte-store-array in general ?
thanks !!!!
link to the app
You can create a derived store, like:
const sorted = derived(todos_, todos => sortBy(todos, 'checkboxed'))
Derives a store from one or more other stores. Whenever those
dependencies change, the callback runs.
(Example)
another solution :
to add that to the TodoLIst component :
$:sortedTodos=$customTodos
.sort((a,b)=>b.checkboxed-a.checkboxed)
.sort((a,b)=>a.line-b.line)
and work with sortedTodos variable in the "each" loop.

Generate new ObjectId using RoboMongo

I have push an object in array of objects mongoose using robomongo
db.getCollection('model').update({_id:ObjectId('')},{$push : {array : {Object}}})
but this didnt created the ObjectId oin document. now i have to manually insert the ObjectId. Question is how to generate it and update the document
You can have new Object ID by ObjectId()
For Example
x = ObjectId()
In this example, the value of x would be:
ObjectId("507f1f77bcf86cd799439011")
for Further
https://docs.mongodb.com/manual/reference/method/ObjectId/

which is best for performance while referring using _id? While retrieving using _id, i am getting error INVALID OBJECT ID

How should i refer a collection in another collection , a unique username or default _id (object id) or a normal id that increments when a new record is inserted . I read that object ids increase performance in mongoose , but i am unable to retrieve records using _ids as its giving error INVALID OBJECT ID . i am not getting error when retrieving using other keys like username . But as _id increases performance i am trying to use that .
Model.find({_id : "idstring"})
I tried these 2 ways while defining schema ,
1) no definition for _id , as it will be created automatically
2) i defined _id like this : _id : { type : Schema.ObjectId }
In both ways, i am getting error "invalid object id" when retrieving records
thanks
You need to create an ObjectID from the string
var ObjectId = require('mongoose').Types.ObjectId;
var myObjectId = ObjectId.fromString('myhexstring');
both 1) and 2) do the same thing. its better to choose 1) and let mongoose do it for you.
i dunno about performance necessarily. the _id field just gets uniquely indexed by mongodb for you so the perf gain in queries would be due to that. you can always index whatever other fields you want if your query is slow. its easier to use _id for sure.
the INVALID OBJECT ID error is due to something being passed that cannot be cast to an ObjectId by mongoose. you can pass a hex string or an instance of mongoose.Types.ObjectId. the hex string should be 24 chars in length. double check this value.
also, explicitly casting the string to an ObjectId is done for you so there is no need to do it manually.

Resources