How to create incremental custom ID in mongoDB - javascript

How can I create a incremental custom id in mongoDB.
Example:
let type = 'x'
let inputCounts = {}
function something(type){
inputCounts[type] = (inputCounts[type] || 0) + 1;
return `custom-id-${inputCounts[type]}`
}
something(type)
Like this what the above function does, I want something that I can do directly in code for mongoDB
So that I can get a customID based on different type, Because like this when I will restart the server the object will get reset.
Thank you

Thank you for the help mates.
Found the solution so thought will share.
Create a counter collection as
db.createCollection("counters")
or
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Counter = new Schema(
{
_id: {type: Schema.Types.ObjectId},
sequence_value: {type: Number}
},
{
timestamps: true
}
);
Counter.index({_id: 1}, {unique: true});
var Counter = mongoose.model('Counter', Counter);
module.exports = Counter;
Then create a JS Function :
function getNextSequenceValue(id) {
return new Promise((resolve, reject) => {
Counter.findOneAndUpdate({_id: ObjectId(id)}, {$inc: {sequence_value: 1}, upsert: true}).exec(function(
err,
counter
) {
if (err) return reject(err);
return resolve(counter.sequence_value);
});
});
}
Then you can get the sequence_value based on id passed.
let custom_id = await getNextSequenceValue(id)
custom_id will give you the value in sequential order

Related

Nodejs mssql bulk insert RequestError: Invalid object name 'table_name'

I am trying to use nodejs mssql package for bulk insert into existing table.It give error as Invalid Object Name 'my_test' even my table is already there and i have tried 'db.schema.tablename' or 'schema.tablename' both. Please help me on that and Thanks in advance. My code part is like below - :
async function getPool(name) {
if (!Object.prototype.hasOwnProperty.call(pools, name)) {
const pool = process.env.NODE_ENV.trim() === 'local' ? new sql.ConnectionPool(configLocal) : new sql.ConnectionPool(config);
const close = pool.close.bind(pool);
pool.close = (...args) => {
delete pools[name]
return close(...args)
}
await pool.connect();
pools[name] = pool;
}
return pools[name];
}
const pool = await getPool('default');
const table = new sql.Table('my_test'); // or temporary table, e.g. #temptable
table.create = false;
table.columns.add('id', sql.Int, { nullable: false,primary:true,identity:true});
table.columns.add('name', sql.VarChar(50), { nullable: false });
table.rows.add(10, 'test');
table.rows.add(11, 'test 2');
const request = new sql.Request(pool);
request.bulk(table, (err, result) => {
console.log("Result ", result, err);//return result;
}); ```

Mongoose nested schema

I want to make a system of topics. each topic may have subtopics which are also topics.
meaning the subtopics may also have subtopics.
for example
example image
I tried to create a topic schema and add a subtopic field which will ref to topic schema too.
cant seem to get this working my schema code:
const mongoose = require('mongoose');
const TopicSchema = new mongoose.Schema({
name: {type:String,unique:true},
sub_topic:[{type:mongoose.Schema.Types.ObjectId, ref : 'Topic'}]
});
const Topic =mongoose.model('Topic', TopicSchema);
module.exports = Topic;
Also what the data im sending to the server should look like to make a new instance?
and on the server how do i save it?
i try to save like this now :
const topic = new Topic();
topic.name = req.body.name;
topic.sub_topic.name=req.body.sub_topic
and the data im sending is :(json)
{
"name":"TestMain",
"sub_topic":[{"name":"TestSub"}]
}
UPDATE : got this done using a recursive function.
function subtopicHandler(topic, sub_topic) {
Topic.find({
"name": topic.name
}, function (err, res) {
if (err) throw err
return;
})
if (sub_topic == undefined) {
let ntopic = new Topic();
ntopic.name = topic.name;
ntopic.sub_topic == undefined;
ntopic.save(function (err, result) {
if (err) console.log('saving err', err)
});
return ntopic._id;
}
let mainTopic = new Topic();
mainTopic.name = topic.name;
sub_topic.forEach(function (sub) {
mainTopic.sub_topic.push(subtopicHandler(sub, sub.sub_topic));
})
var retME;
mainTopic.save(function (err, result) {
if (err) {
console.log('saving err', err)
throw err;
}
});
return mainTopic._id;
}
Using this schema :
const TopicSchema = new mongoose.Schema({
name: {type:String,unique:true},
sub_topic:[{type:mongoose.Schema.Types.ObjectId, ref : 'Topic'}]
});
and data sent as :
{
"name":"A",
"sub_topic":[
{"name":"C","sub_topic":
[
{"name":"C1"}
,
{"name":"C2"}
]
}
,
{"name":"B"}
,
{"name":"D","sub_topic":
[
{"name":"D1"}
,
{"name":"D2"}
,
{"name":"D3"}
]
}
]
}
to the API endpoint
handled this way:
let mainTopic = new Topic();
mainTopic.name = req.body.name;
subtopicHandler(mainTopic, req.body.sub_topic);
})
If you are sending following json
const obj = {
"name":"TestMain",
"sub_topic":[{"name":"TestSub"}]
}
Then,
let mainTopic = new Topic();
let subTopic = new Topic();
// assuming for now you have only one sub-topic in array
subTopic.name = obj[0].name;
subTopinc.save(function(err,result)=>{
if(!err){
mainTopic.name = obj.name;
mainTopic.sub_topic = [result._id]
mainTopic.save(function(err,result){
console.log(result);
})
}
});
From you schema definition and the given json you can follow the above step to get the results.
Hope this will help you.
You can do this with sub docs check out the documentation.
https://mongoosejs.com/docs/subdocs.html

Mongoose returns Mongo object, but can't access property on it

In my simple Node/Mongo/Mongoose setup, I have a function that calls the server to see what the highest ID I'm using currently is, and return the next ID. This function takes as a callback the functionality that creates a new Game.
Weird: The logger.log as it appears below outputs
result { _id: 555d83d5bb0d4e3c352d896f, gameId: 'NaN' }
but when I change the logger to
logger.log("result", result.gameId);
the output is
result { _id: 555d83d5bb0d4e3c352d896f, gameId: 'NaN' }
which makes no sense. Clearly that property is there!
Here is my code
var createGame = function(gameNickname, callback){
nextGameId(function(nextId){
var newgame = new models.Game({
"gameId": Number(nextId),
"gameNickname": gameNickname
});
newgame.save(function(result, game){
callback(result + nextId);
});
});
};
var nextGameId = function(callback){
var games = models.Game.find({}, {gameId: 1});
games.sort('-gameId').limit(1) //get the highest number roundId and add 1 to it
.exec(function (err, result) {
if (err) logger.log(err);
if (result === null){
callback(0);
}
else{
logger.log("result", result);
callback(result.gameId);
}
});
};
i recommend you use autoincrement mongoose plugin, somthing like this
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var connection = mongoose.createConnection("mongodb://localhost/db");
autoIncrement.initialize(connection);
var GameSchema = {
"gameId": {type: Number},
"gameNickname": {type: String}
}
GameSchema.plugin(autoIncrement.plugin, { model: 'Game', field: 'gameId' });
mongoose.model('Game', GameSchema);
after this you can save your game with autoinc, for example:
var Game = mongoose.model('Game');
function createNewGame(nickname){
return new Game({gameNickname: nickname}).save(function(err, res){
console.log(res);
//some code...
})
}
after execute this code you should have somnthing like this:
{
_id: "555d83d5bb0d4e3c352d896f",
gameNickname: "nickname",
gameId: 1
}

Querying a MongoDB based on Mongo ID in a node.js app

I'm using a node.js and mongodb, and I'm trying to query the database based on the mongo generated ID using the following:
collection.findOne( {_id:doc._id} , function(err, item) {});
I am 100% certain that my doc._id is an exact match to the doc _id that I am looking for in the collection, and yet I get a null response from the db query.
I have tried this using other keys in the document and it returns the document just fine. It's only when I try to use the mongo ID.
The MongoDb is an object not a string. To convert my string I used:
var id = require('mongodb').ObjectID(doc._id);
This converts my string into a mongo ObjectId and matches the _id in the db!
Following is the example which spots the issue:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
ObjectID = require('mongodb').ObjectID;
var MongoClient = require('mongodb').MongoClient
//let id = your _id, smth like '6dg27sh2sdhsdhs72hsdfs2sfs'...
var obj_id = new ObjectID('52cbd028e9f43a090ca0c1af');
var justId = '52cbd028e9f43a090ca0c1af'; // <== This will not work
MongoClient.connect('mongodb://127.0.0.1:27017/YourDbName', function(err, db) {
console.log('err' + err);
db.collection('YourCollectionName', function(error, collection) {
//collection.find({_id:justId}),function(err, docs) { // <== This will not work
collection.findOne({_id:obj_id},function(err, docs) {
console.log("Printing docs from Array. count " + JSON.stringify(docs));
});
});
});
Use this:
ObjectId = require('mongodb').ObjectID;
Then when you try to find an object in collection by _id use this:
console.log("find by: "+ id);
database.collection("userRegister").findOne({_id: new ObjectId(id)},
function(err, res) {
if (err) console.log(err);
if(res!=null){
console.log(res)
return false;
}
if(res==null){
callback({'status':_error,'flag':'notexist','message':_userNotExist});
return false;
}
});
First we need to get ObjectID from mongodb library and need to create new instance in following way., so that you will get the ObjectID of string. If your are using es6 in your code this code
import { ObjectID } from 'mongodb';
var emQuery = [
{
$match: {
_id: new ObjectID(tlvaltResult[0].customers.createdBy)
}
},
{
$project: {
_id:1,
emailId:1,
mobile:1
}
}
];
console.log(emQuery,'emQuery');
[ { '$match': { _id: 5ad83ff0b443435298741d3b } },
{ '$project': { _id: 1, emailId: 1, mobile: 1 } } ]
var emResult = await User.getAggregation(emQuery);
console.log(emResult,'emResult');
[ { _id: 5ad83ff0b443435298741d3b,
emailId: 'superAdmin#limitlessmobile.com' } ]
First, ensure you've added all required modules in MongoDB config:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
ObjectID = require('mongodb').ObjectID;
var BSON = require('mongodb').BSONPure;
var server = new Server('localhost', 27017, {
auto_reconnect: true
});
var db = new Db('YOUR_DB_NAME', server);
Then, when you try to find an object in collection by _id, use:
//let id = your _id, smth like '6dg27sh2sdhsdhs72hsdfs2sfs'...
var obj_id = BSON.ObjectID.createFromHexString(id);
db.collection("NAME_OF_COLLECTION_WHERE_IS_YOUR_OBJECT", function(error, collection) {
collection.findOne( {_id:obj_id} , function(err, item) {
// console.log ( item.username );
});
});
Hope, this works.

node.js mongodb select document by _id node-mongodb-native

I'm trying to select a document by id
I've tried:
collection.update({ "_id": { "$oid": + theidID } }
collection.update({ "_id": theidID }
collection.update({ "_id.$oid": theidID }}
Also tried:
collection.update({ _id: new ObjectID(theidID ) }
This gives me an error 500...
var mongo = require('mongodb')
var BSON = mongo.BSONPure;
var o_id = new BSON.ObjectID(theidID );
collection.update({ _id: o_id }
None of these work. How to select by _id?
var mongo = require('mongodb');
var o_id = new mongo.ObjectID(theidID);
collection.update({'_id': o_id});
This the approach that worked for me.
var ObjectId = require('mongodb').ObjectID;
var get_by_id = function(id, callback) {
console.log("find by: "+ id);
get_collection(function(collection) {
collection.findOne({"_id": new ObjectId(id)}, function(err, doc) {
callback(doc);
});
});
}
now you can just use this:
var ObjectID = require('mongodb').ObjectID;
var o_id = new ObjectID("yourObjectIdString");
....
collection.update({'_id': o_id});
You can see documentation here
With native_parser:false:
var BSON = require('mongodb').BSONPure;
var o_id = BSON.ObjectID.createFromHexString(theidID);
With native_parser:true:
var BSON = require('mongodb').BSONNative;
var o_id = BSON.ObjectID.createFromHexString(theidID);
I just used this code in Node.js app in controller file, and it works:
var ObjectId = require('mongodb').ObjectId;
...
User.findOne({_id:ObjectId("5abf2eaa1068113f1e")})
.exec(function(err,data){
// do stuff
})
do not forget to install "mongodb" before, and if you are using encryption of your passwords with bcrypt with "presave", be sure that you will not encrypt password after each modification of the record in DB.
/* get id */
const id = request.params.id; // string "5d88733be8e32529c8b21f11"
/* set object id */
const ObjectId = require('mongodb').ObjectID;
/* filter */
collection.update({
"_id": ObjectId(id)
} )
ObjectId reports deprecated when called inside find() function in "mongodb": "^4.1.2" if the ObjectId is imported like this
const ObjectId = require('mongodb').ObjectID;
instead, when I import it with named import there is no deprecated warning
const { MongoClient, ObjectId } = require("mongodb");
then I can call it regularly
const findResult = await collection.find({_id: ObjectId(id)}).toArray();
This is what worked for me.
Using mongoDB
const mongoDB = require('mongodb')
Then at the bottom where I am making my express get call.
router.get('/users/:id', (req, res) => {
const id = req.params.id;
var o_id = new mongoDB.ObjectID(id);
const usersCollection = database.collection('users');
usersCollection.findOne({
_id: o_id
})
.then(userFound => {
if (!userFound){
return res.status(404).end();
}
// console.log(json(userFound));
return res.status(200).json(userFound)
})
.catch(err => console.log(err));
});`
The answer depends upon the variable type you are passing in as the id. I pulled an object id by doing a query and storing my account_id as the ._id attribute. Using this method you simply query using the mongo id.
// begin account-manager.js
var MongoDB = require('mongodb').Db;
var dbPort = 27017;
var dbHost = '127.0.0.1';
var dbName = 'sample_db';
db = new MongoDB(dbName, new Server(dbHost, dbPort, {auto_reconnect: true}), {w: 1});
var accounts = db.collection('accounts');
exports.getAccountById = function(id, callback)
{
accounts.findOne({_id: id},
function(e, res) {
if (e) {
callback(e)
}
else {
callback(null, res)
}
});
}
// end account-manager.js
// my test file
var AM = require('../app/server/modules/account-manager');
it("should find an account by id", function(done) {
AM.getAllRecords(function(error, allRecords){
console.log(error,'error')
if(error === null) {
console.log(allRecords[0]._id)
// console.log('error is null',"record one id", allRecords[0]._id)
AM.getAccountById(
allRecords[0]._id,
function(e,response){
console.log(response,"response")
if(response) {
console.log("testing " + allRecords[0].name + " is equal to " + response.name)
expect(response.name).toEqual(allRecords[0].name);
done();
}
}
)
}
})
});
If you use Mongosee, you can simplify the function
FindById:
this replace in mongodb: "_id" : ObjectId("xyadsdd434434343"),
example:
// find adventure by id and execute
Adventure.findById('xyadsdd434434343', function (err, adventure) {});
https://mongoosejs.com/docs/api.html#model_Model.findById
I'm using client "mongodb": "^3.6.2" and server version 4.4.1
// where 1 is your document id
const document = await db.collection(collection).findOne({ _id: '1' })
console.log(document)
If you want to copy and paste here's all you need.
const { MongoClient } = require('mongodb')
const uri = '...'
const mongoDb = '...'
const options = {}
;(async () => {
const client = new MongoClient(uri, options)
await client.connect()
const db = client.db(mongoDb)
const document = await db.collection(collection).findOne({ _id: '1' })
console.log(document)
)}()
In Mongoose, the Model.findById() function is used to find one document by its _id. The findById() function takes in a single parameter, the document id. It returns a promise that resolves to the Mongoose document if MongoDB found a document with the given id, or null if no document was found.
const schema = new mongoose.Schema({ _id: Number }, { versionKey: false });
const Model = mongoose.model('MyModel', schema);
await Model.create({ _id: 1 });
// `{ _id: 1 }`
await Model.findById(1);
// `null` because no document was found
await Model.findById(2);
When you call findById(_id), Mongoose calls findOne({ _id }) under the hood. That means findById() triggers findOne() middleware.
const schema = new mongoose.Schema({ _id: Number }, { versionKey: false });
schema.pre('findOne', function() {
console.log('Called `findOne()`');
});
const Model = mongoose.model('MyModel', schema);
await Model.create({ _id: 1 });
// Prints "Called `findOne()`" because `findById()` calls `findOne()`
await Model.findById(1);
Mongoose casts queries to match your schema. That means if your _id is a MongoDB ObjectId, you can pass the _id as a string and Mongoose will convert it to an ObjectId for you.
const _id = '5d273f9ed58f5e7093b549b0';
const schema = new mongoose.Schema({ _id: mongoose.ObjectId }, { versionKey: false });
const Model = mongoose.model('MyModel', schema);
await Model.create({ _id: new mongoose.Types.ObjectId(_id) });
typeof _id; // 'string'
// `{ _id: '5d273f9ed58f5e7093b549b0' }`
const doc = await Model.findById(_id);
typeof doc._id; // 'object'
doc._id instanceof mongoose.Types.ObjectId; // true
Source

Categories

Resources