Update model, delete its array and populate new array using MongoDB Mongoose - mongodb

I wish to ask for advice on the best way to do the following performance-wise.
so, for example, I have a model
person :{
_id: "12312",
name: "hello",
phones: [
{ number : "123456" }
]
}
when I update the person model, like the name: "newname", I also wish to clear the phone array and populate it with a new one.
i was thinking of doing a findById/findOne().deleteArray().populateArray() chaining.
what would be the best path to chain it in Mongoose?
I figure deleting the array is the quickest since the id is a random alphanumeric and not some incrementing long value so might as well delete everything and recreate the array
thoughts?
This is my function in Express/Mongoose
exports.update = function(req, res) {
Person.findByIdAndUpdate(req.params.person_id, person, { new: true, runValidators: true }, function(err, person) {
if (err) res.send(err);
res.json(person);
});
};

You can set the phones array with an empty array with findOneAndUpdate like this:
const newDoc = {
name: "newname",
phones: []
}
let doc = await Person.findOneAndUpdate({ name: newDoc.name }, newDoc, {
new: true
});
console.log(doc);
or if you want to replace existing phones with new ones you can use an object like this:
const newDoc = {
name: "newname",
phones: ["1","2"]
}

Related

Mongooose: How to get a id from a find array

I have a group-based document application, an error is happening in the sharing part. The application is composed of the following parts: Groups, viewers, users and documents.
A group can have many viewers and users, a user can have many documents. I'm working on the part where viewers can see all documents of users associated with the group the viewer is associated with
My controller
router.get("link", async (req, res) => {
const group = await Group.find({ viewer: req.session.userId }).select("id")
console.log(group); // This console.log returns the id in a array: [ { _id: new ObjectId("6323a88670c0dd9aaa5017d2") } ]
console.log(group.id); // This console.log returns undefined
const user = await User.find({ group: group.id });
console.log(user); // Nothing because the group.id is undefined
const documents = await Document.find({
user: user.id,
});
return res.render("page", {
documents,
});
Group schema
name: {
type: String,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true,
},
viewer: [{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
}],
createdAt: {
type: Date,
default: Date.now,
}
I'm not able to retrieve the id from Group.find; what could be happening?
Because you want to have one value. So you can use findOne. Due to using findOne, you can reach group._id.
const group = await Group.findOne({ viewer: req.session.userId }).select("id")
console.log(group); { _id: new ObjectId("6323a88670c0dd9aaa5017d2") }
If you try to take the value from your array, you should take 0. element of array. Because it is an array and has elements. You are trying to reach element's id value.
But which element's id ? You need to declare it. Therefore, need to use group[0]._id. But if you want to reach just one element, using findOne() is better.
const group = await Group.find({ viewer: req.session.userId }).select("id")
console.log(group[0]); { _id: new ObjectId("6323a88670c0dd9aaa5017d2") }
I hope, it is clear and helps you. Good luck

Validate array of strings in mongoose

I wanted to validate each value from the request which is array of strings. Something like
emails: [ 'johndoe#gmail.com', 'jandoe#gmail.com' ]
Here is my schema
const UserSchema = mongoose.Schema({
name: {
type: String,
index: true,
required: true,
},
emails: [String],
});
In my validation I wanted to make sure that each email is not already exists in the database. I've tried the following
body("emails").custom((value, { req }) => {
return User.findOne({
emails: { $all: value },
_id: { $ne: req.params.id },
}).then((exists) => {
if (exists) {
return Promise.reject("Email already exists!");
}
});
});
But the problem is if I tried to post multiple emails in array the validation fails and the data will be inserted to db. How can I check if one of the emails already exists and reject the request?
In the docs of $in, it mentioned that:
If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array...
So you can solve it by:
User.findOne({
emails: { $in: value },
_id: { $ne: req.params.id },
})...

Is an ObjectId automatically generated for a nested object?

My schema is as follows:
const MessageType = {
// ...
oAuth: { provider: String, id: String },
attachments: [ {name: String, contentType: String} ],
// ...
}
MessageSchema = new mongoose.Schema(MessageType, { timestamps: true});
Messages = mongoose.model("Message", MessageSchema);
When I insert a new Message document using Messages.create, an ObjectId (_id) is also generated for attachments, in addition to my name and contentType fields, ie:
[ { name: "xxx", contentType: "yyy", _id: zzzzzz }]
Why is this happening, for attachments but not oAuth?
For avoiding that the _id was generated you must set the option _id: false, Also if you don't want to save the empty attachments object, you need to set default: undefined.
const MessageTypeSchema = new mongoose.Schema({
oAuth: {
type: String
},
attachments: {
type: [
{
type: String
}
],
_id: false,
default: undefined
}
});
Here the code that I used to test:
console.log('-------- Document with attachments --------');
new MessageTypeModel({
oAuth:'xxxxxxxxxxxxx',
attachments: ['teste.png','teste2.jpg']
}).save().then(result => {
console.log(result);
});
console.log('-------- Document without attachments --------');
new MessageTypeModel({
oAuth:'xxxxxxxxxxxxx'
}).save().then(result => {
console.log(result);
});
And here the result of execution:
Mongoose creates _id for single nested subdocuments or arrays, and your object field oAuth is not one of this cases:
Subdocuments are documents embedded in other documents. In Mongoose,
this means you can nest schemas in other schemas. Mongoose has two
distinct notions of subdocuments: arrays of subdocuments and single
nested subdocuments.
Each subdocument has an _id by default. Mongoose
document arrays have a special id method for searching a document
array to find a document with a given _id.
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
// Array of subdocuments
children: [childSchema],
// Single nested subdocuments. Caveat: single nested subdocs only work
// in mongoose >= 4.2.0
child: childSchema
});
Link of Mongoose documentation: Mongoose SubDocs
You can define _id : false in attachments array.
const MessageType = {
// ...
attachments: [ {name: String, contentType: String, _id: false} ],
// ...
}

Creating multiple documents in mongoose only if it does not currently exist

I was wondering if to create multiple documents in mongoose, but only if they do not exist currently? From the documentation, I've found the code below to create multiple documents, but just wondering how to ensure that it does not create a document if it currently exist?
In particular, if one document already exists, I would like the other documents that are not currently created to be created (rather than the entire create operation to fail).
From Documentation
var array = [{ type: 'jelly bean' }, { type: 'snickers' }];
Candy.create(array, function (err, jellybean, snickers) {
if (err) // ...
});
As noted in the documentation, the .create() method is a shortcut function for creating a new document for the given model and "saving" it to the collection. This actually works like the more formal .save() method but in shortcut form.
What you are describing though is more akin to the "upsert" behavior of the MongoDB .update() method. Which can also apply to its .findAndModify cousin or specifically in mongoose, the .findOneAndUpdate() method.
So with some sample code:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/nodetest');
var candySchema = new Schema({
type: String
});
var Candy = mongoose.model( "Candy", candySchema );
var array = [
{ type: 'jelly bean' },
{ type: 'snickers' },
{ type: 'mars' },
{ type: 'snickers' }
];
array.forEach(function(n) {
Candy.findOneAndUpdate( n, n, { upsert: true }, function(err,doc) {
console.log( doc );
});
});
You would see the following output:
{ _id: 535088e2e4beaab004e6cd97, type: 'jelly bean' }
{ _id: 535088e2e4beaab004e6cd98, type: 'snickers' }
{ _id: 535088e2e4beaab004e6cd99, type: 'mars' }
{ _id: 535088e2e4beaab004e6cd98, type: 'snickers' }
Noting that the second entry for 'snickers' actually refers to the object that was already created.
So that is a basic way to ensure that you are not actually creating the same data twice as long as you specify the "key" to match in the query condition.

Mongodb - Get back object instead of array from find

db.users.find();
Will return me an array of users:
[{
_id: 123
name: bob
},{
_id: 456
name: tom
}]
I need to map users to another collection by the id, so I would like to get an object back from mongo where the keys are _id and values are the user doc.
i.e.
users = {
123: {_id: 123, name: bob},
456: {_id, 456, name:tom}
}
Then I can access users directly from that object without having to iterate an array to find specific users.
id = 123;
user = users[id];
You can't get an object like this one from mongodb, but it's quite easy to build it yourself:
db.users.find(function (err, docs) {
var users = {};
docs.forEach(function (doc) {
users[doc._id] = doc;
});
do_whatever_you_want_next(users);
});
Posting my solution in a more modern syntax:
const pushSubscriptions = await PushSubscription.find({ token: { $in: tokens } }).exec();
const userTokens = pushSubscriptions.reduce(
(result, ps) => {
result[ps.token] = ps;
return result;
},
{});