Mongoose - How to add an object in a sub sub document array? - arrays

I can add an item into a sub document array but I can't add an item into a sub sub document array..
Here is my Mongoose schema :
var botSchema = {
"name" : String,
"type" : String,
"keyword" : String,
"active" : Boolean,
"cron" : String
};
var apiSchema = {
"name" : String,
"key" : String,
"secret" : String,
bots:[botSchema]
};
var userSchema = {
"email" : String,
"password" : String,
apis : [apiSchema]
};
module.exports.User = mongoose.model('user',userSchema);
How can I add a bot item to an existing API item ?
I have no problem when adding an API to the User. I do this :
User.findByIdAndUpdate(
userId, {
$push: {
apis: api
}
}, {
safe: true,
upsert: true
},
function(err, model) {
console.log(err);
console.log(model);
}
);
But i 'm trying everything to add the bot to an existing API. I tried this one :
User.findOneAndUpdate({
"_id": userId,
"apis._id": apiId
}, {
$push: {
"apis.$.bots": bot
}
},
function(err, doc) {
console.error("Error:", err);
console.log("Doc:", doc);
}
);
But i got this error shown :
CastError: Cast to undefined failed for value "[object Object]"

Related

Copy a field from one collection to another in mongodb with foreign key as mixed type

Although i have found a similar question on stackOverFlow MongoDB copy a field to another collection with a foreign key
I want to copy a field name from userdetails collection to user collection where userId in userDetails equals _id in user.
user collection
{
"_id" : ObjectId("5b97743bbff66e0be66283cc"),
"username" : "mmi_superadmin",
"accId" : "acc1"
}
{
"_id" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"username" : "client",
"accId" : "acc1"
}
userDetail collection
{
"_id" : ObjectId("5b97743bbff66e0be66283cd"),
"userId" : "5b97743bbff66e0be66283cc",
"name" : "mmi_superadmin"
}
{
"_id" : "5bab8a60ef86bf90f1795c44",
"userId" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"name" : "RAHUL KUMAR TIWARI"
}
Here is my query :
db.userDetails.find().forEach(
function(x) {
db.user.update( {_id :x.userId}, {$set: {name:x.name}});
}
);
This query is partially working. It only updates user documents where _id is of type string. User document with _id as ObjectId are not getting updated.
Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8 not a mongo _id) and use this query:
let usersIds = [];
db.user.find({"_id": {$type: 7}}).forEach(doc => {
usersIds.push(doc._id + '')
db.userDetail.find({
userId: {
$in: usersIds
}
}).forEach(doc => {
db.user.update(
{
"_id": ObjectId(doc.userId)
},
{
$set: {
"name": doc.name
}
},
{
multi: false,
upsert: false
}
)
})
})
if you have any question feel free to ask

Add array values into MongoDB where element is not in array

In MongoDB, this is the simplified structure of my account document:
{
"_id" : ObjectId("5a70a60ca7fbc476caea5e59"),
"templates" : [
{
"name" : "Password Reset",
"content" : "AAAAAAAA"
},
{
"name" : "Welcome Message",
"content" : "BBBBBB"
}
]
}
There's a similar default_templates collection
let accnt = await Account.findOne({ _id: req.account._id }, { templates: 1 });
let defaults = await DefaultTemplate.find({}).lean();
My goal is to find the missing templates under account and grab them from defaults. (a) I need to upsert templates if it doesn't exist in an account and (b) I don't want to update a template if it already exists in an account.
I've tried the following:
if (!accnt.templates || accnt.templates.length < defaults.length) {
const accountTemplates = _.filter(accnt.templates, 'name');
const templateNames = _.map(accountTemplates, 'name');
Account.update({ _id: req.account._id, 'templates.name' : { $nin: templateNames } },
{ '$push': { 'templates': { '$each' : defaults } } }, { 'upsert' : true },
function(err, result) {
Logger.error('error %o', err);
Logger.debug('result %o', result);
}
);
}
This succeeds at the upsert but it will enter all default templates even if there's a matching name in templateNames. I've verified that templateNames array is correct and I've also tried using $addToSet instead of $push, so I must not understand Mongo subdoc queries.
Any ideas on what I'm doing wrong?
Edit: I've gotten this to work by simply removing elements from the defaults array before updating, but I'd still like to know how this could be accomplished with Mongoose.
You can try with bulkWrite operation in mongodb
Account.bulkWrite(
req.body.accountTemplates.map((data) =>
({
updateOne: {
filter: { _id: req.account._id, 'templates.name' : { $ne: data.name } },
update: { $push: { templates: { $each : data } } },
upsert : true
}
})
)
})

How to insert record automatically in mongodb (nodejs, express, mongoose)

I am working on a team-based project. I created a new collection with some records (inserted manually). Is there any script or code to insert these records automatically from within the code, so that my when my colleague will work they do not need to insert those records again.
Code:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ServiceCategoryTypeSchema = new Schema({
_id: {type: String},
name:String
}, {
collection:'service_category_type',
timestamps:{createdAt:'created_at', updatedAt:'updated_at'}
}
);
module.exports = {
getModel: function(db){
return db.model("ServiceCategoryType", ServiceCategoryTypeSchema)
},
schema:ServiceCategoryTypeSchema
};
This is the record, I am thinking to add automatically,
{
"_id" : "Inventory",
"name" : "Inventory"
}
{
"_id" : "Non-inventory",
"name" : "Non-inventory"
}
{
"_id" : "Service",
"name" : "Service"
}
When you have your model in, say, YourModel, then you should be able to save your data that you have in, say, yourData with something like this:
new YourModel(yourData).save(function (error, data) {
// handle errors, log success etc.
});
You can do it for as many pieces of data as you want.
When you populate the database with some data it may be a good idea to first check if the database is not populated yet.
Example
Here is a working example program that saves such data - I changed the database and collection names so that you won't mess with your real database when you run it:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var P = mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://localhost/poptest');
var TestModel = mongoose.model('TestModel', new Schema({
_id: String,
name: String
}, {
collection: 'testcollection',
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
}
));
var sampleData = [
{_id: "Inventory", name: "Inventory"},
{_id: "Non-inventory", name: "Non-inventory"},
{_id: "Service", name: "Service"}
];
P.all(sampleData.map(i => new TestModel(i).save()))
.then(() => console.log('Data saved'))
.catch((err) => console.log('Error: ' + err))
.finally(process.exit);
You need to install mongoose and bluebird for it to work:
npm i mongoose bluebird
It creates 3 documents in the poptest database on localhost. You can verify it by running:
mongo poptest
and querying the testcollection collection:
db.testcollection.find();
You should get something like:
> db.testcollection.find().pretty();
{
"_id" : "Inventory",
"updated_at" : ISODate("2016-09-14T16:13:37.374Z"),
"created_at" : ISODate("2016-09-14T16:13:37.374Z"),
"name" : "Inventory",
"__v" : 0
}
{
"_id" : "Non-inventory",
"updated_at" : ISODate("2016-09-14T16:13:37.377Z"),
"created_at" : ISODate("2016-09-14T16:13:37.377Z"),
"name" : "Non-inventory",
"__v" : 0
}
{
"_id" : "Service",
"updated_at" : ISODate("2016-09-14T16:13:37.377Z"),
"created_at" : ISODate("2016-09-14T16:13:37.377Z"),
"name" : "Service",
"__v" : 0
}
This solution worked for me: https://github.com/Automattic/mongoose/issues/6326
'use strict'
const mongoose = require('mongoose')
const uri = 'mongodb://localhost/test'
const db = mongoose.createConnection(uri)
const Schema = mongoose.Schema
const testSchema = new Schema({
name: String,
age: Number
})
const Test = db.model('test', testSchema)
const test = new Test({ name: 'Billy', age: 31 })
db.once('connected', function (err) {
if (err) { return console.error(err) }
Test.create(test, function (err, doc) {
if (err) { return console.error(err) }
console.log(doc)
return db.close()
})
})

Mongoose update array property

I am trying to update a document via Mongoose, but it does not update the array property.
Here's an example document:
{
"_id" : "55da477a9bfc910e38zzccf2",
"projectname" : "ASong",
"owner" : "adam",
"tracks" : [{
"name" : "Bass",
"file" : "upload/Bass.mp3",
"volume" : "0.75",
"pan" : "0.65"
}, {
"file" : "upload/Drums.mp3",
"volume" : "0.4",
"pan" : "-0.75",
"name" : "Drums"
}
],
"users" : ["adam", "eve"]
}
if I pass to Mongoose an object and try to use it to update like this:
var id = req.body._id;
var editedProject = req.body;
delete editedProject._id;
console.log("TRACKS: " +JSON.stringify(editedProject.tracks));
Project.update({"_id": id}, editedProject, {"upsert": true}, function(err, proj) {
if (err) {
res.send(err);
}
res.json(proj);
});
Only the 1st level properties are updated, but not the array.
I've found some solutions that involve looping through the array elements and calling an update for each element, but I would like to avoid that and keep the update as a single operation.
Or is it better to avoid using arrays?

Inserting elements in array mongoose creates Object in NodeJS

I'm trying to insert documents in an array using Mongoose.
Here's the Schema:
var user = new mongo.Schema({
_id : Number,
devices:[
{
device_id : String,
type : String
}
]})
The update code in NodeJS looks like:
app.put('/user/update',function(req,res){
var obj = req.body;
users.update(
{'user.username' : obj.email},
{
'user.username' : obj.email,
'user.password' : obj.password,
'user.name.first' : obj.name,
'user.department' : obj.department,
'user.year' : obj.year,
'user.college' : obj.college,
'user.contact.phone': obj.contact,
'user.last_login' : obj.last_login,
$push:{
'devices': {
device_id: 'sadasd32u4je3bdjas',
type: 'Windows'
}
}
}, function(err){
if(err)
res.json({"foo": String(err)});
else
res.json({"foo": "Successfully Signed up!"});
});
}
);
But instead it inserts something like:
"devices": [
"[object Object]",
"[object Object]",
"[object Object]",
"[object Object]",
"[object Object]"
],
Where did I go wrong? Thanks again.
Use the findOneAndUpdate() method with the 'upsert' option set to true - this creates the object if it doesn't exist (defaults to false):
var obj = req.body;
var query = {'user.username': obj.email};
var doc = {
$set: {
'user.username': obj.email,
'user.password': obj.password,
'user.name.first': obj.name,
'user.department': obj.department,
'user.year': obj.year,
'user.college': obj.college,
'user.contact.phone': obj.contact,
'user.last_login': obj.last_login
},
$push: {
'devices': {
device_id: 'sadasd32u4je3bdjas',
type: 'Windows'
}
}
};
var options = {upsert: true};
users.findOneAndUpdate(query, doc, options, function(err){
if(err)
res.json({"foo": String(err)});
else
res.json({"foo": "Successfully Signed up!"});
});

Resources