How to get the model/schema name from subdocument? - javascript

I have the following schema and subschema:
The child schema:
const SettingSchema = new Schema({
settingIdentifier: {
type: String,
required: true
},
shareLink: {
type: String,
required: true
},
downloadLink: {
type: String,
required: true
},
direction: {
type: String,
required: true
}
});
And the parent schema:
const DeviceSchema = new Schema({
id: {
type: String,
required: true
},
store: {
type: String,
required: true
},
storeName: {
type: String,
required: true
},
deviceInfo: {
type: String,
required: true
},
contentSettings:{
type:[SettingSchema],
required: true
}
})
In my code, I want to get the child schema name from the retrieved document.
For example, I can get the document as such:
Device.findOne({ deviceInfo: "someValue" }).then(device => {
// device is the retrieved document
// with this device object, how do I get the device[settings] schema name?
// device[settings] is an array of SettingSchema with possibly multiple members
// Expected output : "SettingSchema"
});
So, how do I get the schema name of the subdocument "settings" when I have the "device" object?
Many thanks in advance.
Edit:
I'm attaching the console.log to show what device contains after Device.findOne.
{
"_id": "5ea034d07d865e38c0d4ed6f",
"id": "1a89d59800a72e39",
"store": 1,
"storeName": "Singapore Store",
"deviceInfo": "test device2",
"contentSettings": [
{
"settingIdentifier": "NewYearEvent",
"shareLink": "share1",
"downloadLink": "dl1",
"direction": "D"
},
{
"settingIdentifier": "EasterEvent",
"shareLink": "share2",
"downloadLink": "dl2",
"direction": "H"
}
],
"__v": 0
}
So Device.findOne gets a device instance from the database and it contains the data, but I don't see the metadata to identify the type or schema name of device itself, or its members

Related

CastError: Cast to ObjectId failed for value "{ '$in': [ new ObjectId("6359f421fd4678e2eba3ffee") ] }" (type Object) at path "author" for model "Blog"

I have a blog model:
let blogSchema = new mongoose.Schema({
author: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
title: { type: String, required: true, unique: true },
description: { type: String, required: true, unique: true },
content: { type: String, required: true, unique: true },
likes: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
views: { type: Number, default: 0, required: true },
tags: [],
currentState: { type: String, enum: ['draft', 'published'], required: true, default: 'draft' }}, { timestamps: true });
and a user model which I don't think is necessary to show.
When I try querying for a blog with the author matching a list of userids that the current user is following as such:
let blogs = await Blog.find({ author: { $in: blogIds } })
It gives me the error: CastError: Cast to ObjectId failed for value "{ '$in': [ new ObjectId("6359f421fd4678e2eba3ffee") ] }" (type Object) at path "author" for model "Blog"
How do I get the blogs? I've tried using the $or operator but I would have to loop and it doesn't work anyway. The array values in $in is found correctly as shown in the error, it isn't converting properly though.
The blogids array is [ new ObjectId("6359f421fd4678e2eba3ffee") ]. It is dynamic and is an array of userids that the user follows (OOPS, the name isn't clear)
I think there might be problem in your blogIds array.Try to convert all value inside it with mongoose.Types.ObjectId.
const allIds=blogIds.map(item=>mongoose.Types.ObjectId(item));
Then update your find query.
const blogs = await Blog.find({ author: { $in: allIds } });

How to use mongoose transactions with updateMany?

I am using the mongoose updateMany() method and I also want to keep it a part of transaction. The documentation shows the example of save() where I can do something like Model.save({session: mySession}) but don't really know how to use it with for example Model.updateMany()
UPDATE:
For example I have two models called SubDomain and Service and they look like this respectively:
SUB-DOMAIN
{
name: {
type: String,
required: true,
},
url: {
type: String,
required: true,
unique: true,
},
services: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Service",
},
],
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
}
SERVICE:
{
name: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
price: { type: Number },
tags: { type: Array },
packages: [
{
name: { type: String, required: true },
description: { type: String, required: true },
price: { type: Number, required: true },
},
],
map: { type: String },
isHidden: {
type: Boolean,
required: true,
default: false,
},
sortingOrder: { type: Number },
isForDomain: { type: Boolean, required: false, default: false },
isForSubDomain: { type: Boolean, required: false, default: false },
subDomains: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "SubDomain",
},
],
}
Now the main field here is the services field in SubDomain and subDomains field in Service.
The complicated part😅:
Whenever the user wants to create new service, I want to $push that service's _id into the array of services of all the subDomains inside that new service
And for that, I am using the updateMany() like this:
const sess = await mongoose.startSession();
sess.startTransaction();
const newService = new Service({
_id: mongoose.Types.ObjectId(),
subDomains: req.body.subDomains
...foo
})
await SubDomain.updateMany(
{ _id: { $in: req.body.subDomains } },
{ $push: { services: newService._id } }
);
The problem starts here, of course I can do:
newService.save({session: sess})
but how do I keep my SubDomain's updateMany in the same transaction (i.e sess)
I know my example is difficult to wrap your head around but I have tried to pick a simplest example rather than copying the exact same code which would have been a lot more difficult

MongoDB: Populate or Query for two collections

I have the following Schemas:
let ProjectSchema = new mongoose.Schema({
title: {
type: String
},
employees: {
user: String, // ==> ObjectId of User from UserSchema
role: String,
workload: Number,
},
description: {
type: String
}
});
let UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
trim: true
},
name: {
type: String
},
projects: {
type: Array // ==> ObjectId of Project from ProjectSchema
}
});
What I want to achieve is to get the user by _id or email and to get also all data from his projects like:
{
"user": {
"_id": "asdf1234",
"email": "your#email.com",
"name": "Max"
"projects": {
"title": "Project 1",
"description": "Just a text",
"employees": {
"user": "asdf1234",
"role": "developer",
"workload": 40
}
}
}
}
And I also want to get a specific project by Id and the employee.user field should be populated with the correct user data.
Can I do this with Mongoose populate?
My approach would be to first do a User.findOne() and then check the project.id and to a Project.findOne().
how about in your UserSchema you reference an array of projects, that way when you query for the user you are querying for all the projects tied to the user.
let mongoose = require("mongoose");
let Projects = require("./projects");
let UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
trim: true
},
name: {
type: String
},
projects: {
type: [Projects.schema]
}
});
Then every time you query for the user it comes with the projects associated to the user.

Mongoose/Mongodb: Index Already Exists With Different Options

I am using Mongoose 5.1.7 and attempting to create a compound index across multiple text indexes in my defined schema. Here is my schema definition:
const mongoose = require('mongoose');
const alumniSchema = mongoose.Schema({
firstName: {
type: [String],
required: true
},
lastName: {
type: [String],
required: true
},
classYear: {
type: Number,
required: true
},
photoURL: {
type: String,
},
education: [
{
school: {
type: String,
required: true
},
gradYear: {
type: Number,
required: true
},
degreeType: String,
degreeSubject: String,
}
],
jobs: [
{
employer: {
type: String,
required: true
},
position: String,
startDate: Date,
endDate: Date,
isCurrent: Boolean
}
],
contactInfo: {
phoneNumber: {
type: String,
},
email: {
type: String,
}
},
})
alumniSchema.index({ firstName: 'text', lastName : 'text', email: 'text' });
module.exports = mongoose.model('Alumni', alumniSchema);
When I boot up the server, I receive the following error:
MongoError: Index: { v: 2, key: { _fts: "text", _ftsx: 1 }, name: "firstName_text_lastName_text_email_text", ns: "5b3be578c0c6e317f7c1bc2b_test.alumnis", background: true, weights: { email: 1, firstName: 1, lastName: 1 }, default_language: "english", language_override: "language", textIndexVersion: 3 } already exists with different options: { v: 2, key: { _fts: "text", _ftsx: 1 }, name: "firstName_text_lastName_text_classYear_text_education.school_text", background: true, weights: { classYear: 1, education.school: 1, firstName: 1, lastName: 1 }, default_language: "english", language_override: "language", ns: "5b3be578c0c6e317f7c1bc2b_test.alumnis", textIndexVersion: 3 }
I have been messing around with this for a while and evidently previously created an index. When I use the mongo shell to check the indexes that I currently have set up, however, I cannot find the index "firstName_text_lastName_text_classYear_text_education.school_text" referenced by the error message:
> db
test
> db.collection.getIndexes()
[ ]
I am at an impasse--I'm not sure if I've incorrectly created the index, or if I am supposed to drop the index (it doesn't look like Mongoose natively supports a dropIndex() function).
Has anyone else dealt with this issue? Thanks!
Looks like Mongoose dynamically creates the index at runtime. The trick for me was adding:
var Alumni = mongoose.model('Alumni', alumniSchema);
Alumni.collection.dropIndex('firstName_text_lastName_text_classYear_text_education.school_text', function(err, result) {
if (err) {
console.log('Error dropping index!', err);
}
});
and then restarting the server.
THEN I was able to change the index to whatever I wanted. Note that I still needed to add the above code segment and restart the server every time I wanted to update the index.

Mongoose, enforce changes to schema after mongoose.model('core_user').schema = new mongoose.schema(...)

I am using the Express-Restify-Mongoose library to generate all of my endpoints for a REST api.
I am storing my schemas in a collection named 'core_schema.' Each time the server starts all records within that collection are grabbed using a find() method. This provides an array of all available collections with defined schemas.
db.on('open', function(){
findDocs('core_schema', {}, function(docs){
var tempString = JSON.stringify(docs);
var tempObj = JSON.parse(tempString);
for (var i = 0; i < tempObj.length; i++) {
var temp_schema = new mongoose.Schema(tempObj[i].schema_data, { collection: tempObj[i].collection_name });
var temp_model = mongoose.model(tempObj[i].collection_name, temp_schema);
restify.serve(router, temp_model, { plural: false, name: tempObj[i].collection_name, preCreate: setDateTimes });
}
});
});
I would like to be able to make changes to the schema to enforce new POST rules, but when I try to use a test example like:
mongoose.model('core_user').schema = new mongoose.Schema({
update_count: { type: "String", required: false },
created_date: { type: "String", required: false },
created_by: { type: "String", required: false },
updated_date: { type: "String", required: false },
updated_by: { type: "String", required: false },
first_name: { type: "String", required: true },
last_name: { type: "String", required: true },
username: { type: "String", required: true },
password: { type: "String", required: true },
somethingElse: { type: "String", required: true }
}, {collection: 'core_user'})
Where 'somethingElse' is the new field I am trying to enforce it does not enforce it. What am I doing wrong? Is there no way to update the model to use the new schema?
To update a Mongoose schema, you'll need to use the Schema.add method. You can update the schema for a model using the code below:
mongoose.model('core_user').schema.add({ somethingElse: {
type: 'String',
required: true
}});

Categories