Generate new ObjectId using RoboMongo - node.js

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/

Related

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.

convert array to mongo ObjectID.....nodejs

I have an object in nodejs :
let id = {"0":94,"1":132,"2":212,"3":194,"4":74,"5":37,"6":102,"7":242,"8":31,"9":49,"10":96,"11":178}
I want to convert this to a mongo ObjectId. I tried this:
const ObjectID = require('mongodb').ObjectID
var objectId = new ObjectID(id);
This throws an error saying:
Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
How do I convert my array to a valid mongo ObjectID? Thanks
Why do you want to convert Object to Mongo Object Id?
Are you sure you want to convert this Object to Mongo Object Id?
https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/
I may be wrong. But usually people store that kind of data to mongodb collection and reference that collection with dynamically created MongoID.

Mongoose not updating key value pairs stored with Object Schema

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();

Deleting a field with $unset operation it just converting it to a null value

I'm trying to delete some fields on my mongodb database but instead the fields are just getting mutated to a null value and that's an issue because when fetching data im using loops and they get broken passing true null value ... for deleting data i'm using the following :
User.updateOne({_id : user._id } ,{ $unset :{ "days.0.tasks.0" = "" })
what i'm getting :
Is there a way to delete entirely the objects inside the tasks array?
$unset is not working as you'd expect it because you are using it on an element inside an array. Removing the element at position 0 means shifting all other elements in the array. To prevent messing with those other elements, $unset is setting the element at position 0 to null.
MongoDB docs
I suggest using the $pull operator to delete specific elements from an array or use $pop for the first / last items .
OR :
you can get the user tasks array and splice it then push it back using $set operator.

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