Mongoose - Keep models associated on changes - javascript

When I have two Schemas and they refer one another like this:
const SchemaA = new Schema({
_schemaB: [{
type: Schema.Types.ObjectId,
ref: 'SchemaA'
}]
});
const SchemaB = new Schema({
_schemaA: {
type: Schema.Types.ObjectId,
ref: 'SchemaB'
}
});
mongoose.model('SchemaA', SchemaA);
mongoose.model('SchemaB', SchemaB);
Every time I create a document of the type SchemaB, I need to add it to the collection of SchemaA to keep it updated.
To achieve it, I'm using a pre.save(...) hook in the SchemaB, but I was wondering if there is a better way to do it.
Thank you!

The only issue I see with using pre hook is what will happen if pre hook succeeds and then actual save fails. In that case you may consider using mongoose transaction to make sure data are saved as one atomic operation.
const session = await SchemaB.startSession();
session.startTransaction();
try {
// save new SchemaB
// add to SchemA and save SchemaA
await session.commitTransaction();
session.endSession();
} catch (e) {
await session.abortTransaction();
session.endSession();
}

Related

Mongoose search by array entries ($in) behavior not aligned with MongoDB Atlas

I'm running a Node.js server, connecting to a MongoDB database with mongoose.
Inside my controller, I have several methods that make operations to the database. One of them is this one:
async findMultiple(req, res) {
const [baseSkillsArray] = Array(req.body);
try {
// if there is not baseSkillsArray, skip
if (!baseSkillsArray) {
return res.status(200).send([]);
}
// find all baseSkills using the ids in the baseSkillsArray
const allBaseSkills = await BaseSkill.find({
_id: { $in: [baseSkillsArray.baseSkillArray] } //
});
console.log('test ' + allBaseSkills);
res.status(200).send(allBaseSkills);
} catch (error) {
console.error(error.message);
res.status(500).send('Server error find BaseSkills');
}
}
However, this returns me nothing. I did some debugging and I found the reason is the find id $in the array. So I tried hard coding a value, like '2', for instance.
// find all baseSkills using the ids in the baseSkillsArray
const allBaseSkills = await BaseSkill.find({ _id: { $in: ['2'] } });
No success. So I went to MongoDB Atlas, where my DB is stored. I tried filtering using the same line of code in my collections.
{ _id: { $in: ['2'] } }
Surprisingly, it returns my document as I wanted!
The issue is that I need to make it work with mongoose. Any ideas? Is this a known bug?
There is nothing wrong with the query, nor a bug regarding $in.
In fact, what's wrong is the actual collection name. I manually created a collection in MongoDB Atlas, called "baseSkills". However, mongoose by default transforms your collection name into lowercase and adds an "s" if your collection's name is not in the plural.
So every time I started my server, I noticed that there was a new collection called "baseskills". I assumed it was a bug and deleted it. Only after making this post that I realized the collection was there again.
So I exported the documents to this collection and my query was working fine.
FYI, there is a way to enforce the collection's name in mongoose. When you declare you model, add a second parameter to the Schema function called "collection". Here is an example:
const BaseSkillSchema = new mongoose.Schema({
_id: {
type: String,
required: true
}, ...
}, { collection: 'baseSkills' })
That's it! Sorry for the mess and thank you for your help!
you want to query over mongo db object ids. So you should create a new ObjectId to do that.
import {Types} from 'mongoose';
{ _id: { $in: [new Types.Object("2")] } }
Or if you have 2 ids one generated and one custom created as id then you can query without creating a new object.
{ id: { $in: ['2'] } }

Sequelize user.increment("somethings") do not update the value

when I try to increment something with sequelize it doesn't return me the updated value (incremented) and to get that I need to reuse findOne method.
Looking in the sequelize doc here in the increment section you can read this:
The updated instance will be returned by default in Postgres. However,
in other dialects, you will need to do a reload to get the new values.
Is ther any way to "fix" it without using this "workaround" ( recalling findOne )?
const user = await Tags.findOne({
where: { id: ID },
});
await user.increment("rank");
const user1 = await Tags.findOne({
where: { id: ID },
});

document wont save using save() method with mongoose

I have this schema:
var UserSchema = mongoose.Schema({
analytic: {
type: Object,
default: {
today:[],
weekly:[],
monthly:[],
yearly:[],
allTime:[]
}
}
});
let User = mongoose.model("bloger", UserSchema);
module.exports = {User};
and I am trying to save some data into one of the arrays like so:
User.findOne({username:username}, (e, user) => {
if (e) {
res.send('error fetching post')
}
else if (!user) {
res.send('no user found')
}
else if (user) {
user.analytic.today.push(req.body.visitor) // push the data object to the array
user.save((e, doc) => {
if (e) {
res.send(e)
}
if (doc) {
console.log('user saved')
res.send(doc)
}
})
}
})
})
I am getting the doc object on save() and not the e so I though it should have save it but it wasn't.
I have had a similar issue before this is because I am not defining a new Model I am just passing a JSON object.
Instead of saving the object you need to create a new model and save that.
Try creating a new model passing the save into it like below;
var newUser = new User(user);
newUser.save((e, doc) {
if (e) {
res.send(e)
}
if (doc) {
console.log('user saved')
res.send(doc)
}
});
Making sure you require the User Model inside the script.
Performing deep modifications in objects not in your schema makes Mongoose oblivious to those changes, preventing it from knowing what to save (and from making efficient atomic updates). The end result is that, when calling .save, Mongoose thinks there's nothing modified and therefore does nothing.
In your particular scenario, you have two options:
1. Add your analytics sub-arrays to your schema
This is the best option and allows for finer control of everything:
const UserSchema mongoose.Schema({
analytic: {
today: [{}],
weekly: [{}],
monthly: [{}],
yearly: [{}],
allTime: [{}],
}
});
With this, those arrays are now known to Mongoose and everything should work correctly.
Note that you don't need defaults in this case, as Mongoose is smart enough to create the arrays as needed.
2. Manually mark modified object as modified
If for any reason you don't want or can't modify the schema, you can manually mark the analytic object as modifies so Mongoose knows of it:
user.analytic.today.push(req.body.visitor) // push the data object to the array
user.markModified('analytic'); // mark field as modified
user.save(...);
This signals Mongoose that analytic or any of its children have changed and triggers an update when calling .save. Note however that Mongoose views this as a full change in the object, while with option 1 it can use $push instead.

Save Or Update Mongoose

Is there a nice way of either saving, or updating a document in mongoose? Something like what I'm after below
let campaign = new Campaign({
title: req.body.title,
market: req.body.market,
logo: req.body.logo,
additional_question_information: question,
status: status
});
campaign.saveOrUpdate().then(function() { ... }
Thanks for the help all
I think what you're looking for is called an 'upsert'.
You can do this by using findOneAndUpdate and passing the { upsert: true } option, something like the below example:
let campaign = new Campaign({
title: req.body.title,
market: req.body.market,
logo: req.body.logo,
additional_question_information: question,
status: status
});
Campaign.findOneAndUpdate({
_id: mongoose.Types.ObjectId('CAMPAIGN ID TO SEARCH FOR')
}, campaign, { upsert: true }, function(err, res) {
// Deal with the response data/error
});
The first parameter to findOneAndUpdate is the query used to see if you're saving a new document or updating an existing one. If you want to return the modified document in the response data then you can also add the { new: true } option.
Documentation here for findOneAndUpdate: http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
You can use the MongoDB's findAndModify function. In mongoose this is natively supported by calling findOneAndUpdate(). Here is the documentation for it. http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate
Notice that in the third argument it awaits for an object to be passed with options. You want to use { upsert : true } in there to create a new document if one does not exist.

How to query linked documents in mongodb

I'd like to know if there's a way to directly search within a linked collection.
I'm using Express with mongoosejs
I have the following situation.
I have 3 collections, deal, product and store and I have associated product and store with the deal collection.
const DealSchema = new Schema({
...
product: {
type: Schema.Types.ObjectId,
ref: 'product'
},
shop: {
type: Schema.Types.ObjectId,
ref: 'shop'
},
...
});
In my collection product, I have a field called upc.
I have a controller that handles deals creation, however before I create a deal, I want to check whether or not there's already a deal with the same UPC in this store if so I'll only update the confirmedBy field.
This is my controller
async create(req, res) {
const dealProps = req.body;
const product = await Products.findOne({ upc: dealProps.upc });
let deal;
if (product) {
deal = await Deals.findOne({ product: product._id });
if (deal.shop.toString() === dealProps.shop.toString()) {
const updatedDeal = await Deals.findOneAndUpdate({ product: product._id }, {
$push: {confirmedBy: dealProps.user }
});
res.send(updatedDeal);
}
} else {
deal = await (new Deals(dealProps)).save();
res.send(deal);
}
}
I've tried to search directly within the product collection like this:
const product = await Deals.findOne({'product.upc': dealProps.upc });
However it returns null.
Is there a way to search directly within a linked collection? Do I need to create an index for the upc field in the product collection?
If not, should I rethink my deals collection to add the upc and storeId to simplify the lookup?
Thank you for your help, much appreciated.

Categories