I need a property in a mongoose model that is as following:
customRanks should have different keys and different values for each key
const config = new Schema({
id: { type: String, required: true, unique: true },
//...other property
customRanks: Object,
});
You can defined custome rank schema and use this as a object if you want to save data in specific format for customRanks fields
const customRanksSchema = new Schema({
key1: { type: Number, default: 0 },
key2: { type: Number, default: 0 },
key3: { type: String, default: '' }
})
If you want static schema and have specific fields for your custome rank object
// if you want to save customeRanks as array of object
customRanks: [{ type: customRanksSchema }],
// if you want to save custom rank as a object
customRanks: customRanksSchema
If you are looking for validation without key specifically. Then you may need to validate data before insert/create/update using javascript.
Related
I have the following schema:
const { ObjectId } = Schema.Types
const Account = new Schema({
name: { type: String, required: true },
accountId: { type: String, required: true },
piracicaba: [{ type: ObjectId, ref: 'Piracicaba'}]
})
I know that by default MongoDB creates an _id property, but I would like to make accountId my default ID instead. Also, I would like to change that default generated string to a custom one.
How could I do that?
I have tried to change the accountId type to ObjectId:
const { ObjectId } = Schema.Types
const Account = new Schema({
name: { type: String, required: true },
accountId: { type: ObjectId },
piracicaba: [{ type: ObjectId, ref: 'Piracicaba'}]
})
But then when I try to store my customized ID mongoose throws BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer.
My customized ID is from Oracle Cloud Account and it looks like this: ocid1.test.oc1..<unique_ID>EXAMPLE-compartmentId-Value
If you include an _id field in your schema definition, when you insert a document you must supply it with your own manually generated _id. If you don't, the document will not get inserted.
Alternatively, if you do not include an _id field in your schema definition, Mongoose will create this for you automatically, when the document is inserted, and it will be of type ObjectId (which is the default way that MongoDB sets the _id field on documents).
I'd like to access a specific value from an array enum in mongoose. I am fully aware that i can get the whole enum by DocumentModel.schema.path('user_role').enumValues but in the context below, i'd like to access the value Buyer under user_role.
The Schema looks like :
const userSchema = new mongoose.Schema({
user_role: {
type: String,
default: "Supplier",
enum: ["Buyer", "Supplier"],
},
user_fullName: {
type: String,
required: true
},
)}
I have a collection named Project. Below is the schema description for Project collection :
const projectSchema = new mongoose.Schema(
{
projectName: {
type: String,
trim: true
},
licenceType: {
type: String,
trim: true,
lowercase: true
},
location: {
type: String,
lowercase: true,
trim: true
},
description: {
type: String,
trim: true
},
projectType: {
type: String,
default: 'public'
},
status: {
type: String,
default: 'open'
},
budget: {
type: Number
},
duration: {
type: Number
},
},
{
timestamps: true
}
);
I want to fetch documents using these fields as filters. The fields are :
licenceType
location
date (timestamp is made true in schema for this purpose)
I can use these three fields in any combination to fetch documents. There is a possibility that no filter is applied at all in which case its a simple fetching of all the documents in the collection.
I know I can dynamically build query using if--else if--else but I wanted to know is there any other more efficient way of handling such queries rather than using if--else blocks. If there would have been five or more fields for filtering purpose, there would be so many combinations to check using if--else block.
Appreciate any kind of help!!Thank You.
So, I assume there's some external trigger which actually modifies the filter matrix (eg. a request from some UI). Mongoose allows you to specify filter up-front in a form of an object, take a look below:
const query = Project.find({
licenceType: 'sometype',
location: 'somelocation'
});
Clearly you can see this...
{
licenceType: 'sometype',
location: 'somelocation'
}
...is an object. So, I think you could re-build the filtering object each time filters change (create an empty object let myFilters = {} and extend it with your filters: myFilters['licenceType'] = 'sometype') and pass myFilters to find function.
I wanted to insert multiple documents in my MongoDB collection. I was able to do that by using Model.collection.insert function but when I insert those data it skip/bypass required validation.
I've tried Model.collection.insert([{data: '1'}, {data: '2'}, {type: '3'}]) but this way it's skip or bypass the validation. I want data field required and I used in my Schema that as required. But that's not working.
There is my schema that required a field.
export const SubjectSchema = new mongoose.Schema({
title: { type: String, required: [true, "title field required"] },
groups_id: { type: String },
class_id: { type: String },
meta: { type: Object }
},
{ timestamps: true })
Here is my function
async createSubject(body) {
let result = SubjectSchema.collection.insert(body)
return result
}
I want multiple data to be stored and in each record, title field should be required
Model.insertMany([{data: '1'}, {data: '2'}, {type: '3'}])
you can find the insertMany ref here
however, you can also db.collection.validate()
I have data that looks like this:
{
name:'name'
email:'email'
password:'pass'
role:{checked: {'1' : true, '2': true ... any number of key value pairs}}}
}
How do I create mongoose schema for this kind of data
?
var CustomUserSchema = new Schema({
name: String,
email: { type: String, lowercase: true },
role: {
checked: { type: Number, type: boolean } //I need to define something like this here probably
},
hashedPassword: String,
provider: String,
salt: String
});
How do I define the schema for this? Basically I am storing the checkbox values in role field.
Checkbox fields have their ng model set to user.role.checked[$index]