mongoose: update field, push object in array [duplicate] - node.js

This question already has answers here:
Stop Mongoose from creating _id property for sub-document array items
(7 answers)
Closed 7 years ago.
I would like to add an element in an array in a mongo database:
db.keypairs.update( {pubkey: "1234567890"}, { $push: {listTxId: {txHash: "yyy", spent: false} } } )
The result is perfect:
listTxId" : [ { "txHash" : "xxx", "spent" : true },{ "txHash" : "yyy", "spent" : false } ]
Now I would like to do the same with node.js and mongoose
var res = wait.forMethod(Keypair,'update', {pubkey: "1234567890"}, { $push: { "listTxId": {"txHash":"zzz", "spent":false} } } );
Keypair is my node.js model for the mongoose collection:
var Keypair = require('./app/models/Keypair');
and wait.forMethod comes from a node module:
var wait = require('wait.for');
In the result, I have this "_id" element :
{ "txHash" : "zzz", "spent" : false, "_id" : ObjectId("56561571fea5d9a10a5771fd") }
QUESTION: where this ObjectId come from ? How can I get rid of it ?
UPDATE: mongoose schema:
var keypairSchema = mongoose.Schema({
userId : { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
pubkey : String,
privkeyWIF : String, // temp
balance : Number,
listTxId : [{
txHash : String,
spent : Boolean
}],
walletId : { type: mongoose.Schema.Types.ObjectId, ref: 'Wallet' },
description : { type: String, maxlength: 40 },
comments : String,
isMasterKey : { type: Boolean, default: false },
date : Date
});

Mongoose will put ids in your subdocument arrays. listTxId is a subdocument array. You can add _id: false to your schema to prevent this:
var keypairSchema = mongoose.Schema({
userId : { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
pubkey : String,
privkeyWIF : String, // temp
balance : Number,
listTxId : [{
_id: false,
txHash : String,
spent : Boolean
}],
walletId : { type: mongoose.Schema.Types.ObjectId, ref: 'Wallet' },
description : { type: String, maxlength: 40 },
comments : String,
isMasterKey : { type: Boolean, default: false },
date : Date
});

Related

Populated nested schema in Mongoose

This is my Schema, I have a CampaignStat Object which has a nested Product Schema, but at the same time Product is another model.
const ProductsSchema = new Schema({
_id: {
type: mongoose.Schema.ObjectId,
ref: 'Product'
},
clickedAt: Date,
deviceInfo: {
type: Schema.Types.Mixed
}}, { _id: false })
const CampaignStatSchema = new Schema({
campaign: {
type: mongoose.Schema.ObjectId,
ref: 'Campaign'
},
subscriber: {
type: mongoose.Schema.ObjectId,
ref: 'Subscriber'
},
openedAt: Date,
products: [ProductsSchema]
}, {
timestamps: { createdAt: true, updatedAt: false }
})
An this is how is being stored in MongoDB
"_id" : ObjectId("5f18d01ecab4eb6175cc0f83"),
"subscriber" : ObjectId("5ed6890da2c99843280a0058"),
"campaign" : ObjectId("5ed937829ff2a1f99de55150"),
"products" : [
{
"_id" : ObjectId("5f160ca2014be4e159f32fcd")
},
{
"_id" : ObjectId("5f160ca2014be4e159f3302e")
},
{
"_id" : ObjectId("5f160ca2014be4e159f33036")
},
{
"_id" : ObjectId("5f160ca2014be4e159f3311a")
},
{
"_id" : ObjectId("5f160ca2014be4e159f33159")
}
],
"createdAt" : ISODate("2020-07-22T23:47:42.228Z"),
"__v" : 0
I need to populate Products, I'm doing this but I cannot get the Product collection attributes
const CampaignStats = await this.find({ campaign: "5ed937829ff2a1f99de55150" })
.populate('subscriber')
.populate({
path: 'products',
model: 'Product'
})
In case somebody gets into the same problem, I changed this (_id for product):
const ProductsSchema = new Schema({
product: {
type: mongoose.Schema.ObjectId,
ref: 'Product'
},
clickedAt: Date,
deviceInfo: {
type: Schema.Types.Mixed
}}, { _id: false })
And my query is like this:
this.find(query)
.populate({
path: 'products',
populate: {
path: 'product',
model: 'Product'
}
})

I tried to find the user from the mongoose db

This is my mongoose schema for movieLibrary
const mongoose = require('mongoose')
const movieSchema = new mongoose.Schema({
movieName:{
type: String,
required: true,
trim: true
},
movieCast:[{
actor:{
type: String,
required: true
},
actress:{
type: String,
required: true
}
}],
yearOfRelease:{
type : Date,
default : Date.now
},
movieDirector:{
type: String,
required: true
},
genre:{
type: String,
required: true
},
rating:{
type: Number,
required: true
}
})
module.exports = mongoose.model('movieLibrary',movieSchema)
I want to find the movie detail by passing the actor name as a parameter
I tried this:-
const movies = await Movie.find({
"movieCast": {
$elemMatch: {
"actor": actor
}
}
})
But i get empty array as the output , Can you help me what issue might have occured , I get the correct answer in robo 3T, but not getting in postman.
I don't see anything wrong in your implementation. I have done the same thing like you except I have hard coded the value in query itself. It's working fine for me.
Data which is available in DB,
{
"_id" : ObjectId("5ed9c47b39d18ba11f4c8f10"),
"movieName" : "Titanic",
"movieCast" : [
{
"actor" : "Leonardo",
"actress" : "Winslet"
}
],
"yearOfRelease" : ISODate("1994-04-01T23:20:19.199Z"),
"movieDirector" : "James Cameroon",
"genre" : "History",
"rating" : 8.8
}
And the query impl,
async test() {
let result = await Test.find({
"movieCast": {
$elemMatch: {
"actor": 'Leonardo'
}
}
});
console.log(JSON.stringify(result));
return result;
}
I think either you could have passed the wrong actor value or none is passed.

How to use populate in node js

I have to populate students in my data,
"attendances" : [
{
"_id" : ObjectId("5851702b9accfc6e70d55107"),
"students" : [
{
"student" : ObjectId("5850e6d596d4de3c1f4a0614"),
"_id" : ObjectId("5851702b9accfc6e70d55109"),
"ispresent" : "No"
}, }]
My schema,
attendances: [{
date: {
type: String,
default: ''
},
created_by: {
type: Schema.ObjectId,
ref: 'Employee'
},
students: [{
student: {
type: Schema.ObjectId,
ref: 'Student'
},
ispresent: {
type: String,
default: ''
}
}]
}],
Attendance.findById(id).populate('attendances.students')
I tried the above way but fails.Can any one please suggest help.Thanks.
You can write your population predicate by the dot notation while you're digging deeper into both object or array to refer at the end to another valid schema of type Schema.Types.ObjectId, so according to your mentioned schema, you query would be as follow:
Attendance.findById(id).populate('attendances.students.student')

Mongoose dup key

I'm getting a duplicate key error and not sure why.
I have the following schema:
var walletSchema = mongoose.Schema({
currencyName : {type : String, required : true, unique : true},
amount : {type : Number, default : 0}
}, {strict : false});
// define the schema for our user model
var userSchema = mongoose.Schema({
local : {
username : { type: String, required: true, unique: true },
password : { type: String, required: true, unique : true },
email : { type: String, required: true, unique: true },
country : { type: String, required: true },
inventory : {
food : { type : Number, default : 0},
energyDrinks : { type : Number, default : 0 }
},
wallet : [walletSchema],
lastAttackedAt : { type: Date, default: Date.now },
lastJobChange : {type: Date, default: '03/30/1988' },
lastWorked : {type: Date},
referredBy : {type : String, default : 'Admin'},
energy : { type: Number, default: 100 },
energyUpdatedAt : { type : Date, default: Date.now },
resetPasswordToken: String,
resetPasswordExpires: Date
}
},{timestamps : true});
I create a new user with this code :
...
newUser.local.username = capitalizeUser(username);
newUser.local.password = newUser.generateHash(password);
newUser.local.email = req.body.email;
newUser.local.country = req.body.country;
newUser.local.wallet.push({
// Create the default currencies
currencyName: 'Euro',
}, {
currencyName: 'Gold',
}, {
currencyName: result.countryCurrency
}
);
// save the user
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
Everything works fine for the first user however if I try to make another user I get MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: xyz.users.$local.wallet.currencyName_1 dup key: { : "Euro" }.
Why is this happening, doesn't each user has it's own wallet? How should I handle it, keep in mind that there are about ~230 currencies available for each user.
currencyName : {type : String, required : true}
Remove unique from there and you will be good to go. Mongo checks unique keys for collection. In your case walletSchema collection will have a lot of same values so that's why it's gives error.
As your currencyName has been set unique so it has to be different for each user you save. In fact you with this schema you won't even be able to have two users from the same country.
So to avoid this you need to remove the unique keyword from you schema and it is done. It then looks something like this.
var walletSchema = mongoose.Schema({
currencyName : {type : String, required : true},
amount : {type : Number, default : 0}
}, {strict : false});

How to set limit for array size in Mongoose schema

Would you be kind to tell me is there any way to set limitation on array size while creating Mongoose schema. For example
var peopleSchema = new Schema({
name: {
type: String,
required: true,
default: true
},
/* here I want to have limit: no more than 10 friends.
Is it possible to define in schema?*/
friends: [{
type: Schema.Types.ObjectId,
ref: 'peopleModel'
}]
})
With a small tweak to your schema setup you can add a validate option:
var peopleSchema = new Schema({
name: {
type: String,
required: true,
default: true
},
friends: {
type: [{
type: Schema.Types.ObjectId,
ref: 'peopleModel'
}],
validate: [arrayLimit, '{PATH} exceeds the limit of 10']
}
});
function arrayLimit(val) {
return val.length <= 10;
}
starting from mongo 3.6 you can add validation for a collection at server end, each document inserted/updated will be validated against the validator $jsonSchema, only the valid gets inserted, validation error will be for invalid documents
db.createCollection("people", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name" ],
properties: {
name: {
bsonType: ["string"],
description: "must be a string"
},
friends: {
bsonType: ["array"],
items : { bsonType: ["string"] },
minItems: 0,
maxItems: 10,
description: "must be a array of string and max is 10"
}
}
}
}
});
collection
> db.people.find()
valid document
> db.people.insert({name: 'abc' , friends : ['1','2','3','4','5','6','7','8','9','10']})
WriteResult({ "nInserted" : 1 })
invalid document
> db.people.insert({name: 'def' , friends : ['1','2','3','4','5','6','7','8','9','10', '11']})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
find
> db.people.find()
{ "_id" : ObjectId("5a9779b60546616d5377ec1c"), "name" : "abc", "friends" : [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ] }
>
You can use $slice modifier at the time you push new friend id into your array
https://docs.mongodb.com/manual/reference/operator/update/slice/#up._S_slice
$push: {
friends: {
$each: [id],
$slice: -10
}
}
This is my schema and array limit on assignedToId via an external function.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const taskSchema = new Schema({
parentTask: {
trim: true,
type: Schema.Types.ObjectId,
ref: "task",
},
assignedToId: [{
trim: true,
type: Schema.Types.ObjectId,
ref: "Employees",
}],
createdBy: {
trim: true,
type: Schema.Types.ObjectId,
ref: "Employees",
required: [true, "User ID is required"]
},
createdByName: {
trim: true,
type: String,
required: [true, "Creater name is required"]
},
},
{
timestamps: true
});
// Validations for assignedTo employees' size
taskSchema.path('assignedToId').validate(function (value) {
console.log(value.length)
if (value.length > 10) {
throw new Error("Assigned person's size can't be greater than 10!");
}
});
const Tasks = mongoose.model("Tasks", taskSchema);
module.exports = Tasks;

Resources