Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Local MongoDB _id

I'm creating an offline app that create users locally and when the app is online I want to sync the created users to my remote mongodb database. So my question is there any plugin preferably in angular that creates a local mongodb _id?

like image 930
Millenial2020 Avatar asked Oct 24 '25 19:10

Millenial2020


1 Answers

You can use any unique id for a _id field. However, if you do not specify _id field at all in your data, MongoDB will itself create a _id field with ObjectID type in documents.

Still, if you need to create ObjectId in your application, you can do it on the server.

It depends on the driver you are using for MongoDB connectivity.

If you are using NodeJS driver for MongoDB, then you can do it like this.

Reference

var ObjectID = require('mongodb').ObjectID;
var objectId = new ObjectID();

If you are using mongoose for MongoDB object modeling in NodeJS, then you can do it like this

Mongoose Reference for types

var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();

For Python MongoDB driver, refer this - pymongo ObjectId

I hope you can create ObjectId using other drivers as well.

like image 138
Tarush Arora Avatar answered Oct 26 '25 07:10

Tarush Arora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!