How to query data from mongoDB with moongoose in nodejs - node.js

I only want to check if the key exist in the databse but when i try to query it I only get NULL.
This is my invite.js
var mongoose = require('mongoose');
mongoose.createConnection('mongodb://localhost/invitation');
var db2 = mongoose.createConnection;
// User Schema
var InvitationSchema = mongoose.Schema({
key: {
type: String,
index: true
},
used: {
type: String
}
});
var Invitation = module.exports = mongoose.model('Invitation', InvitationSchema);
module.exports.getUsedByKey = function(id, callback){
var query = {used: key};
Invitation.findById(query, callback);
};
module.exports.getInvitationByKey = function(key, callback){
var query = {key: key};
Invitation.findOne(query, callback);
console.log('keythingy ' + callback);
};
And this is how I try to use that function:
function checkKey(key, res) {
Invitation.getInvitationByKey(key, function(err, key2) {
//console.log('key: ' + key + '\nkey2: ' + key2.key)
if (err) throw err;
if (key2 !=null) {
return key2.key;
} else {
return false;
}
})
}

Use the below best way for write code in NodeJS. Mongoose.
Make one JavaScript class for connection with mongodb like --> db.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/invitation');
var Schema = mongoose.Schema;
// User Schema
var InvitationSchema = mongoose.Schema({
key: {
type: String,
index: true
},
used: {
type: String
}
}, {
collection: 'Invitation'
});
module.exports.Invitation = mongoose.model('Invitation', InvitationSchema);
Make another fetch data from collection JavaScript class --> invitation.js
var db = require('db.js');
module.exports.Initation = {
getInvitationByKey : function(key, callback) {
var query = {key: key};
db.Invitation.findOne(query, function(err, result){
console.log("Result : ", result, ", Error : ", err);
callback(err, result);
});
}
}
Make route JavaScript class --> invitation_route.js
var express = require('express');
var router = express.Router();
var invitation = require('invitation.js').Initation;
router.get('/get/:key', function(req, res, next) {
invitation.getInvitationByKey(req.params.key, function(err, result){
if (err) {
console.log(' Error : ', err);
res.body(err);
} else {
console.log(' Result : ' + JSON.stringify(result));
res.send(result);
};
});
});

Related

How to get data by dynamic collection name in nodejs and mongoose

Tried to get data from table by passing collection name dynamically but not working.I have two table[shoptable and markettable] and same header data.I want to get data depends on passing collection name.I do not know how to set schema for dynamic collection.Anyone can find wher i did mistake?
table.controller.js //Nodejs
const mongoose = require('mongoose');
const _ = require('lodash');
var Schema = mongoose.Schema;
let dynamicModels = {};
const ConvForUserSchema = new Schema({
product_name: {
type: String
},
price: {
type: String
},
catogery: {
type: String
}
}, {
versionKey: false,
strict: false
});
const dynamicModel = (collectionName) => {
if (!(collectionName in dynamicModels)) {
dynamicModels[collectionName] = mongoose.model(collectionName, ConvForUserSchema, collectionName);
}
return dynamicModels[collectionName];
};
module.exports.getTableData = (req, res, next) => {
let collection = req.query.collection;
console.log("col=" + collection)
dynamicModel(collection).find({}, function(err, docs) {
if (err) {
console.log('ss' + err);
return
}
return res.json(docs);
})
}
user.service.ts:
getTableData(collection){
return this.http.get(`http://localhost:3000/api/getTableData?collection=${collection}`);
}
product.component.js:
shopDatas(){
this.getProductsData('shoptable');
}
marketDatas(){
this.getProductsData('markettable');
}
getProductsData(collection){
this.userService.getTableData(collection).subscribe(
res => {
this.tableData = res;
},
err => {
console.log(err);
}
);
}
product.component.html:
<button (click)="shopDatas()">Shop data</button>
<button (click)="marketDatas()">Market data</button>
const newCollection = mongoose.model("collectionName" , Schema)
newCollection.find({} , (err, docs)=>{
console.log(docs)
})

Mongoose pagination from server side

I am trying to add server side pagination to a NodeJS, Express and MongoDB API. The API use mongoose to handle the database. I am lost in how to customize the response from the Controller.
Model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const clientSchema = Schema({
code: {
type: String,
required: [true,'Code no puede estar vacio']
},
name: {
type: String,
required: [true,'Name no puede estar vacio']
}
},{
timestamps: true
});
const Client = module.exports = mongoose.model('clients',clientSchema);
Controller for get all clients:
const mongoose = require("mongoose");
const Client = require('../models/client');
const clientController = {};
clientController.index = (limit, callback) => {
Client.find(callback).limit(limit);
};
module.exports = clientController;
Route to get the clients:
app.get('/api/clients', (req, res) => {
Client.index(limit,(err, client) => {
if (err) {
res.status(500).json({
msg: "Error en aplicacion",
err
});
}
res.status(200).json(client);
});
});
How can I customize the result in the controller to something like this:
[
{
"totalRecords":"99999999999",
"offset":"888888",
"page":"4",
"nextPage":"5"
"result":{...}
}
]
I already have a function to calculate the pagination, But I don't know how to add the information about the pagination in the result of the controller.
Before I was adding the pagination data in the route, But I want to handle the pagination logic in the controller.
Or is better handle the pagination in the route?
Thanks in advance
You can create a method in mongoose model called as paginate :
Add this before declaring mongoose model :
clientSchema.methods.paginate = function(pageNo, callback){
var limit = 10;
var skip = pageNo * (limit - 1);
var totalCount;
//count documents
this.count({}, function(err, count)){
if(err){
totalCount = 0;
}
else{
totalCount = count;
}
}
if(totalCount == 0){
return callback('No Document in Database..', null);
}
//get paginated documents
this.find().skip(skip).limit(limit).exec(function(err, docs){
if(err){
return callback('Error Occured', null);
}
else if(!docs){
return callback('Docs Not Found', null);
}
else{
var result = {
"totalRecords" : totalCount,
"page": pageNo,
"nextPage": pageNo + 1,
"result": docs
};
return callback(null, result);
}
});
});
const Client = module.exports = mongoose.model('clients',clientSchema);
Then in controller change :
app.get('/api/clients', (req, res) => {
//You could put page number in request query ro request params
Client.paginate(req.body.pageNo, function(err, response) {
if (err) {
return res.status(500).json({
message : "Error en aplicacion",
error : err
});
}
return res.status(200).json(response);
});
});

How to save nested array in MongoDB using Mongoose and NodeJS

Can anyone explain me how to save the nested array items into mongodb with mongoose and nodejs?.
Here is a schema I am using.
var demoSchema = ({
"r_id": Number,
"r_label": String,
"entity": [{
"d_label": String,
"d_type": String
}
]
})
And here is Nodejs function I am using to save the data into db
app.route("/mypages/rooms")
.post(function(req, res) {
var db = mongoOp.demo();
var response = {};
req.checkBody("r_id", "Enter a valid r_id address.").notEmpty();
req.checkBody("r_label", "Enter a valid label address.").notEmpty();
var errors = req.validationErrors();
if (errors) {
console.log(errors);
console.log(req.body);
res.status(500);
res.end('500 Server Error');
//res.render('addrooms',{flag:1});
return;
} else {
db.r_id = req.body.r_id;
db.r_label = req.body.r_label;
db.entity = req.body.entity;
db.save(function(err) {
if (err) {
findfromdb(req, res, 2); //own function for implementation purpose
} else {
findfromdb(req, res, 1);
}
});
//var middleVar = req.body.resources;
// console.log(middleVar[0].d_rgb);
}
});
set entity with array []
db.entity = [{}];
app.route("/mypages/rooms")
.post(function(req, res) {
var db = mongoOp.demo();
var response = {};
req.checkBody("r_id", "Enter a valid r_id address.").notEmpty();
req.checkBody("r_label", "Enter a valid label address.").notEmpty();
var errors = req.validationErrors();
if (errors) {
console.log(errors);
console.log(req.body);
res.status(500);
res.end('500 Server Error');
//res.render('addrooms',{flag:1});
return;
} else {
db.r_id = req.body.r_id;
db.r_label = req.body.r_label;
db.entity = [{
"d_label": req.body.label_type,
"d_type": req.body.d_type
}];
db.save(function(err) {
if (err) {
findfromdb(req, res, 2); //own function for implementation purpose
} else {
findfromdb(req, res, 1);
}
});
//var middleVar = req.body.resources;
// console.log(middleVar[0].d_rgb);
}
});
The below operation adds the element label_type and d_type to entity array if they does not exist in the array, if they exists, then they won't be added
https://docs.mongodb.com/manual/reference/operator/update/addToSet/
Model.update(
query, // { _id: 1 }
{
$addToSet: {
"enity": {
"d_label": req.body.label_type,
"d_type": req.body.d_type
}
}
}
)
have a look at this answer
Pushing item to Mongodb collection array

Mongoose not returning document from mongo

I am trying to get Mongoose to query my mongo instance for a specific document (using the _id attribute). I currently have the following route:
router.get('/document/', function (req, res) {
var getDocuments = function (callback) {
var options = { server: { socketOptions: { keepAlive: 1000 } } };
var connectionString = 'mongodb://user:password#server:27017/db';
var pID = req.query.id;
pID = pID.trim();
console.log(pID);
var documentArray = [];
// Connected handler
Mongoose.connect(connectionString, function (err) {
var db = Mongoose.connection.useDb('db');
var pCollection = db.collection("collection");
//grab all items from pCollection
pCollection.find({ '_id': pID }, function (error, pDocument) {
if (error) {
console.log(error);
}
if (pDocument) {
// res.send(JSON.stringify(pDocument));
console.log(pDocument);
}
else {
console.log("nothing");
}
});
db.close();
});
};
getDocuments(function () {
});
});
The result returned is not a json document and does not seem to return a usable value. What am I doing wrong? Thanks for any help in advance!
EDIT:
I went back and changed the route to the following:
router.get('/document/', function (req, res) {
var pID = req.query.id;
pID = pID.trim();
console.log(pID);
Document.findById(pID, function (error, document) {
if (error) {
console.log(error);
}
else {
console.log(document);
}
});
});
I also created the following model:
var mongoose = require('mongoose');
var DocumentSchema = require('../schemas/documents');
var Document = mongoose.model('documents', DocumentSchema, 'Documents');
module.exports = Document;
And used the following schema:
var mongoose = require('mongoose');
var DocumentSchema = new mongoose.Schema({
documenttPK: String,
locationID: String,
docName: String,
SerialNumber: String,
documentID: String,
dateEntered: Date,
activeFlag: Boolean
});
module.exports = DocumentSchema;
My app.js makes a single call to a db file:
var mongoose = require('mongoose');
mongoose.connect('mongodb://user:password#server:27017/db');
But the result is still null. Is something wrong with the above code?

MongoDB Node JS Driver find toArray getting the Object Id in different format

I am trying to get all documents from a collection as follows:
var server = "localhost";
var port = 27017;
var dbName = "myNewCreation";
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var connString = "mongodb://"+server+":"+port+"/"+dbName;
mongoClient.connect(connString, function(err, db) {
console.dir(err);
if(!err) {
var collectionName = "employee";
//get Documents
db.collection(collectionName).find({}).toArray(function (err, docs) {
console.dir(err);
console.dir(docs);
});
}
else{
console.log("Mongo DB could not be connected");
process.exit(0);
}
});
I am getting the results correctly, but the output of the Object ID is in some machine format as follows:
[ { _id: ObjectID { _bsontype: 'ObjectID', id: 'VX%\u000fÜÏ¢6©ø' },
empName: 'riyke',
Dob: '20-31-2300',
salary: '7000' } ]
How to get the Object ID properly in human readable form?
I found the solution and it is working fine, I am just updating my question with answer:
Use toString or toHexString methods as follows:
var server = "localhost";
var port = 27017;
var dbName = "myNewCreation";
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var connString = "mongodb://"+server+":"+port+"/"+dbName;
mongoClient.connect(connString, function(err, db) {
console.dir(err);
if(!err) {
var collectionName = "employee";
//get Documents
db.collection(collectionName).find({}).toArray(function (err, docs) {
console.dir(err);
console.dir(docs);
consolde.dir(docs[0]._id.toString());
consolde.dir(docs[0]._id.toHexString());
});
}
else{
console.log("Mongo DB could not be connected");
process.exit(0);
}
});
you can use valueOf() method , ObjectId.valueOf() it will return hex string

Resources