How to mongoose model based on dynamic schema passed as parameter? - javascript

I am new on mongoose and for expressjs
I want to retrieve a collection based on the doc and model.
I have multiple schema that inherits a common schema.
const extendSchema = (schema: mongoose.Schema<any>, definition: any): Schema<any> => {
return new mongoose.Schema({ ...schema.obj, ...definition, ...{ strict: false }});
};
const CommonSchema = new mongoose.Schema({ ... });
const OtherSchema = extendSchema(CommonSchema, { ... });
const OtherOtherSchema = extendSchema(CommonSchema, { ... });
Then, I want to retrieve the collection from the mongoose
const getCollectionObject = (collection: string, schema: Schema) => {
return collection.model(collection, schema);
};
// get the first collection
export const getOtherCollection = async (name: string, id: string) => {
try {
const model = getCollectionObject(name, OtherSchema);
const document = await model.findById(mongoose.Types.ObjectId(id)).lean();
return document;
} catch (error) {
return error;
}
};
// get the second collection
export const getOtherOtherCollection = async (name: string, id: string) => {
try {
const model = getCollectionObject(name, OtherOtherSchema);
const document = await model.findById(mongoose.Types.ObjectId(id)).lean();
return document;
} catch (error) {
return error;
}
};
I've got an error below
Is it possible?
Thank you in advance!
PS: I've already saw other posts which the solution is to make the properties optional.

This solved my issue.
Create a common schema plus the other schema
const CommonSchema = new mongoose.Schema({ ... });
const OtherSchema = { ... };
const OtherOtherSchema = { ... };
Then, I declared my based model.
const Base = mongoose.model('collection-name', CommonSchema);
Next, I created my other models based on the base model using discriminator
const OtherModel = Base.discriminator("Other", new mongoose.Schema(OtherSchema));
const OtherOtherModel = Base.discriminator("OtherOther", new mongoose.Schema(OtherOtherSchema));
You can now use the model on any scoped function, you may export it if you want to.
Other.create({ ... });
Other.findById()
OtherOther.create();
OtherOther.findById();
please let me know if this right approach
or you have any other suggestions
Thanks!

Related

mongodb full $text search returns duplicate items when sorting and filtering

I am using full-text search in my API and sorting using the score. The issue is after searching I usually get an item more than once and this is not the behaviour i expected. Please, how can i correct this?
here is how my router is designed
router.get('/place', async (req, res) => {
const match = {
deactivated: false,
}
const sort = {}
if (req.query.search) {
match.$text = {$search: req.query.search}
sort.score = {$meta: "textScore"}
}
const noOnPage = parseInt(req.query.limit) || 20
const pageNo = (parseInt(req.query.page)-1)*parseInt(req.query.limit) || 0
const endIndex = parseInt(req.query.page)*parseInt(req.query.limit)
const next = parseInt(req.query.page)+1
const previous = parseInt(req.query.page)-1
try {
const count = await Place.find(match).countDocuments().exec()
const place = await Place.find(match, sort)
.limit(noOnPage)
.skip(pageNo)
.sort(sort)
const result = {}
// Shows the search result count
result.resultCount = count
// Shows the previous page number
if (parseInt(req.query.page)!=1) {
result.previous = previous
}
// Shows the next page number
if (endIndex < count) {
result.next = next
}
// assigns the search results to variable names results
result.results = place
res.status(200).send(result)
} catch (e) {
res.status(400).send({ "message": "something went wrong please reload page" })
}
})
You could try using the mongoose paginate module.below are the simple changes you would need to make.
Example User model:
import {model, Schema} from 'mongoose';
const mongoosePaginate = require('mongoose-paginate-v2');
const userSchema = new Schema({
firstname: { type: String,trim:true},
lastname: { type: String,trim:true},
isconfirmed:{type:Boolean,default:false},
},
{timestamps:true});
userSchema.plugin(mongoosePaginate);
const appUsers:any = model('appusers', userSchema);
export default appUsers;
Sample get all users method, note the sort can be passed in options. You can play with the options object as you please
getAllUsers(req:Request,res:Response){
const options = {page: req.body.page?req.body.page:1,limit: 10,collation:
{locale: 'en'},sort:{createdAt:-1},select: {_id:1,firstname:1,lastname:1}};
appUsers.paginate({}, options, function(err:any, result:any) {
if(err){
res.status(500).json({status:false,msg:"Failed to fetch All
users",err:err});
}else{
res.status(200).json({status:true,msg:"List populated",data:result});
}
});
}

How to add mongoose transaction and create a document?

I want to add a mongoose transaction in the POST method. When creating the transaction it should be creating a document called stock. Can anybody help me figure out what should I do here? I have a node/express/mongoose app with the following:
GoodsRecivedNote controller
router.post('/', async (req, res) => {
const session = await mongoose.startSession()
try {
const _id = await getNextSequence('goodsReceivedNote')
req.body.id = _id
const goodsReceivedNote = new GoodsReceivedNote(req.body)
const stocks = new Stock(req.body)
await goodsReceivedNote.save()
//use mongoose transaction
//creates a loop(data get from the arry called cart in goodsrecivednote)
for (const item of data) {
//insert stock modal(orderNo, packingId, orderSize, poNumber)
item.create({})
//insert(data, {session})
}
await session.commitTransaction()
res.sendStatus(200)
} catch (error) {
await session.abortTransaction()
return res.sendStatus(500)
} finally {
session.endSession()
}
})
GoodsRecivedNote model
const goodsReceivedNoteSchema = new Schema(
{
id: Number,
poNumber: String,
orderedDate: String,
supplier: String,
orderNo: String,
cart: [
{
packingId: Number,
actualSize: String,
orderSize: String,
brandId: Number,
artWork: String,
receivedQty: Number,
grnDate: String,
},
],
},
)
module.exports = mongoose.model(
'GoodsReceivedNote',
goodsReceivedNoteSchema
)
Stock model
const stockSchema = new Schema(
{
id: Number,
poNumber: Number,
orderNo: String,
packingId: Number,
orderSize: String,
receivedQty: Number,
availableQty: Number,
},
)
module.exports = mongoose.model(
'Stock',
stockSchema
)
Maybe you can try something like this
const session = await mongoose.startSession()
session.startTransaction()
const opts = { session }
const stocks = await new Stock(req.body).save(opts)
await goodsReceivedNote.save(opts)
...the rest of your code
When ever you call Save Update or Delete please add opts as option
Answer by 21bn gets the work done but withTransaction() is way better than startTransaction().
I recommend you use withTransaction instead.
const session = await mongoose.startSession();
await session.withTransaction(async (session) => {
// For create..
collection.create({something:"something"},{session:session});
});
For insertmany, updatemany, the rule is basically the same..
collection.updateMany({find_something:"something"},{name:"Some name"},{session:session});
If you want to find a document using session..
collection.findOne({_id: "some_id"}).session(session));

how to do getter to the root object in js?

In JavaScript, how to do getter to object in the root level?
I mean how to create getter for this object:
const userModel = {
get() { return { foo: true, ... }; }
}
and access to it by userModel.foo; // true. the code gives me userModel.get().. which not expect what I need.
The idea is go invoke get when I access to userModel.
Is it possible to do with js?
I am using the typeorm library and I have models for example User.
typeorm is able me to use this model by using this syntax:
import { getConnection } from 'typeorm';
const b = await getConnection().getRepository(User).findOne({...})...
now I want to create UserModel that do this:
getConnection().getRepository(User)
as:
export const userModel = {
get() { return getConnection().getRepository(User) }
}
so can I use it as:
userModel.findOne({...});
And I can't just use:
export const userModel = getConnection().getRepository(User);
Because the connection is not initialized yet, so it throws an error.
Yes, it is possible using Proxy:
const getConnection = (model) => ({
getRepository: (model) => {
return { findOne: () => (model) }
}
});
const User = {
//...user model,
name: "Jonh Smith"
}
class ProxyModel {
constructor(model) {
this.model = model;
return new Proxy({}, {get: (function(target, name) {
const repo = getConnection().getRepository(this);
if(name in repo) {
return repo[name];
}
}).bind(this.model)})
}
}
const userModel = new ProxyModel(User);
console.log(userModel.findOne());
Make sure you define getter properly:
const userModel = {
get foo() { return { foo: true }; }
}
console.log(userModel.foo)

How can I loop through this array inside this instance of my mongoose model?

const postSchema = new mongoose.Schema({
post:[
{postId: String},
{commentComponent: [
{comment: [String]},
]}
]
})
const Posts = mongoose.model('Posts', postSchema)
This is the definition of the schema for modeling the mongodb
const postLinks = await getPostLinks();
const posts = new Posts({
for (let i = 0; i < postLinks.length; i++) {
const comment = await getComment(postLinks[i]) // here it takes postLinks as a paramaeter to get an array of comment
post: [
{postId: postLinks[i]},
{commentComponent: [
{comment: comment}
]}
]
}
})
const result = await posts.save()
is there a way of iterating inside this instance because the for loop here is not working
You need to pass an object to the Posts constructor with a property called post (which probably should be called posts, but will keep the original name below), and for this property, you need to specify an array.
This array can be built by using Array.prototype.map and Promise.all:
const post = await Promise.all(
postLinks.map(async (postLink) => {
const comment = await getComment(postLink);
return {
postId: postLink,
commentComponent: [{ comment }],
};
})
);
const posts = new Posts({ post });
const result = await posts.save();
But if you prefer, you can use the traditional for-loop (more similar to what you were trying to do) as well:
const post = [];
for (let i = 0; i < postLinks.length; i++) {
const comment = await getComment(postLinks[i]);
post.push({
postId: postLinks[i]},
commentComponent: [{ comment }]
});
}
const posts = new Posts({ post });
const result = await posts.save();
Based on your code example I am not certain what you are attempting to do. When using a Model and trying to create you can think of as a new singular record. If you are trying to insert many links into a single record I would suggest comma separating them then inserting that into your MongoDB.
But you cannot iterate inside your Posts class like that.
If I were you I would set up my file something like this:
file: models/Post.js:
const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
text: {
type: String,
trim: true,
required: [true, 'Please add some text']
},
link: {
type: String,
required: [true, 'Please add link']
},
createdAt: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('Post', PostSchema);
Then create a controller js file
file: controllers/posts.js:
const Post = require('../models/Post');
// #desc Add Post
// #route POST /api/v1/posts
// #access Public
exports.addPost = async (req, res, next) => {
try {
// get post data from the request
// mongo returns a promise so await on it
const post = await Post.create(req.body);
return res.status(201).json({
success: true,
data: post
});
} catch (err) {
if(err.name === 'ValidationError') {
const messages = Object.values(err.errors).map(val => val.message);
return res.status(400).json({
success: false,
error: messages
});
} else {
return res.status(500).json({
success: false,
error: 'Server Error'
});
}
}
}
then in your router file, you can use your controller:
routes/post.js
const express = require('express');
const router = express.Router();
const { addPost } = require('../controllers/posts');
router
.route('/')
.post(addPost);
module.exports = router;

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