findOneAndUpdate doesn't create ObjectId - mongodb

I need to make a patch request to update only one (or several) field(s) at the same time.
I've got a big object which is my document, and inside nested array of objects.
For example, for my car array, this is the schema :
const carSchema = new Schema({
owner: [{value: {type: String}, label: {type: String}}],
carPlate: {type: String},
carColor: {type: String},
carBrand: {type: String},
carStatus: {type: String}
});
const myObject = new Schema({
...
cars: [carSchema]
...
});
When I send my changes, I do it this way :
let dynamicVar = 'cars.'+i+'.'+myfield;
this.props.updateGeneral({_id: this.props.general._id, [dynamicVar ]: [myfield.value]});
I'm on redux, so my action looks like :
export function updateGeneral(data) {
let _id = data._id;
delete data._id;
return {
type: 'UPDATE_GENERAL',
payload: client.patch(`${url}/${_id}`, data)
}
}
And my PATCH request is like :
router.patch('/url/:id', async (req, res, next) => {
myObject.findOneAndUpdate({_id: req.params.id}, {$set: req.body }, {upsert: true, new: true}, function (err, objectReturn) {
if (err) return next(err);
cn = cn.substr(0, cn.indexOf(' {'));
res.json(objectReturn);
});
});
My BIG issue is that my field is update or inserted, but if it's inserted and it creates a new array it won't create the objectId linked. It won't even create the array of object,just an object with a property.
How can I make mongoose initiates ObjectId??

Per the reply to this SO post it looks like you cannot update object IDs. When doing so, you are effectively "deleting" the object and creating a new one.

Related

Mongoose Populate a field

I have two mongoose schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var itemSchema = new Schema({
name: {type: String, required: true, max: 25, trim: true},
price: {type: Number, required: true, trim: true, default: 0},
tax: {
type: Schema.Types.ObjectId,
ref: "Store"
}
});
module.exports = mongoose.model('Item', itemSchema);
The second Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var storeSchema = new Schema({
name: {type: String, required: true, trim: true},
taxes: [
{
name: String,
rate: Number
}
]
});
module.exports = mongoose.model('Store', storeSchema);
What I want to do is populate the itemSchema tax object with the storeSchema taxes array of object. every time I pushed a new tax object to the taxes array mongoose created an ObjectId. I stored that ObjectId in my itemSchema Tax. I want to use that _id to retrieve the store taxes that matches the itemSchema _id.
I have tried this so far, but I get no in the tax attribute.
Item.find().populate("tax", "taxes").exec(function (err, docs) {
if (err) return console.error(err);
console.log(items);
});
Try this query
Item.find().populate({
path: 'tax',
select: 'taxes'
}).exec(function (err, docs) {
if (err) {
console.error(err);
} else {
console.log(docs);
}
});
Item.find().populate(path:"tax", model: )
Mention your item model file name... don't use "" or '' for the file name, simply add the file name.
Use Item.find().populate("Store", "taxes") instead of Item.find().populate("tax", "taxes").

Mongoose why would you use populate over another find?

I'm guessing because you save resources by making 1 request instead of 2 to the database. Is this significant? Should I care to use populate if I'm populating only 1 field (the advantage is clearer when you populate more than 1)?
You don't save resources by using populate. Under the hood mongoose calls the database as many times as required. Consider the example:
module.exports = function(){
var UserSchema = new Schema({
email : {type : String, required: true},
password: {type: String, required: true}
});
return mongoose.model("User", UserSchema);
};
module.exports = function(){
var AccountSchema = new Schema({
number : {type : String, required: true},
user: {type: Schema.Types.ObjectId, ref: 'User'}
});
return mongoose.model("Account", AccountSchema);
};
mongoose.set('debug', true); //to see queries mongoose is using behind the scenes
Account.find({}, function(err, res){
console.log(res)
}).populate("user")
Apart from the results, you'll see something like this on console:
Mongoose: accounts.find({}, { fields: undefined })
Mongoose: users.find({ _id: { '$in': [ ObjectId("5807d6d6aa66d7633a5d7025"), ObjectId("5807d6d6aa66d7633a5d7026"), ObjectId("5807d709aa66d7633a5d7027") ] } }, { fields: undefined })
That's mongoose finding account documents and then user for each one of them.
It's saving you a lot of code and I don't see why you should not use it irrespective of the number of fields you're populating.

mongodb relations find one with all their related

I know how to get data with their related
var UserSchema = new Schema({
username: String
});
var PostSchema = new Schema({
title: String,
author: {type: Schema.Types.ObjectId, ref: 'User'}
});
...
Post.findOne({_id: id})
.populate('author')
.exec(function(error, result) {
// do stuff
})
but how to do the opposite?
I mean when I want a user, with all their posts with single query?
Try adding a filter step after the query returns that manually filters out documents which don't have any posts that matched the populate criteria:
var username = "foo";
Post.find()
.populate('author', null, {username: username})
.sort({'_id': 1})
.exec(function (err, posts) {
posts = posts.filter(function(post){
return post.author.length;
});
res.send(posts);
});

MongoDB $pull value from array of ObjectIDs

I have this document in my collection:
{
"_id" : ObjectId("52718433e18a711923000005"),
"owners" : [
ObjectId("52718433e18a711923000004"),
ObjectId("52ed40dccc5bc50000000003"),
ObjectId("52ed4171abe2780000000003")
]
}
I have the following statement, where I am trying to remove one of the values in owners field:
Business.update({_id:req.body._id}, {$pull:{"owners":req.body.userid}}, function(err){
if(err){
res.json(500, {message:"Could not remove user from admin list"});
}else{
res.json(200);
}
});
I know that req.body._id and req.body.userid have valid values:
{ _id: '52718433e18a711923000005',
userid: '52ed4171abe2780000000003' }
Other operations, such as finding business by ID, etc, work, so it's not an ObjectId format issue. What else could it be?
--
Edit: here is my (abbreviated) schema definition:
var BusinessSchema = new Schema({
business_name: {type: String, required: true},
owners: {type: Array}
});
Your current schema doesn't provide any direction to Mongoose regarding the data type contained within the owners array field. If you want Mongoose to cast your string to an ObjectID you need to provide type information in your schema:
var BusinessSchema = new Schema({
business_name: {type: String, required: true},
owners: [{type: Schema.ObjectId}]
});
It looks like a conversion to ObjectId is required when trying to match values to pull. But not to search. So this works:
var ObjectId = require('mongoose').Types.ObjectId;
Business.update({_id:req.body._id}, {$pull:{"owners": new ObjectId(req.body.userid)}}, function(err){
if(err){
res.json(500, {message:"Could not remove user from admin list"});
}else{
res.json(200);
}
});
--
Edit: So, if I change my schema from owners: {type: Array} to owners: [Schema.Types.ObjectId], I can skip the conversion, and my original statement works.

Sort Nested document in MongooseJS

This is my schema:
var Review = new Schema({
user: {type: ObjectId, ref:'User'},
lastModified: {type: Date, default: Date.now }
});
var Subject = new Schema({
name: String,
review: [Review],
...
});
The query will return all the subjects with review from a user.
{'review.user': id}
Is it possible to sort the result based on review.lastModified?
Subject.find({'review.user': id}).select('name').sort('???').exec( function(err, subjects){
if (err) return res.json(error);
console.log('subject', subjects);
});
You cannot sort within a document using MongoDB. Sorting within the document must be done at the application level.