Accessing a Value in ENUM in a mongoose Schema - node.js

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
},
)}

Related

How to change, customize, Mongoose default _id property?

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).

object as property mongoose mongodb

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.

Is there a way to query a document based on a subdocument object ref?

Suppose we have a schema that looks like this:
const RandomSchema = new Schema({
name: String,
randomField: String,
subDoc: {
name: String,
refDoc: {
type: Schema.Types.ObjectId,
ref: 'OtherModel',
required: true,
},
},
}, options);
Our OtherModel has a schema that looks like this:
const OtherModel = new Schema({
name: String,
funFact: String,
}, options);
From the front end of my application I'd like to query the RandomSchema model and return all instances of this model where subDoc.refDoc.funFact === someValue.
Is this possible? I know we have ways to populate those subdocs when return them but it happens only after matching docs have been returned, when in this case we'd need to know more than just the objectId of refDoc.
If multiple collections are involved, this task requires use of aggregation pipeline.

Mongoose - store multiple model types in one field

I have a schema which looks like this:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const timestamps = require('mongoose-timestamps');
const BidSchema = new Schema({
bidder: {
type: Schema.ObjectId,
ref: 'Buyer'
},
counterOffer: {
type: Schema.ObjectId,
ref: 'Counter_Offer'
},
box: {
type: Schema.ObjectId,
ref: 'Box'
},
status: {
type: String,
enum: ['Pending', 'Approved', 'Disapproved']
},
bidPrice: Number,
bidTimeout: {
type: Date,
default: Date.now
},
isAnonymous: Boolean,
});
BidSchema.plugin(timestamps);
module.exports = mongoose.model('Bid', BidSchema);
I want to store multiple data types in the Box field. For example, I have a Box model and also I have a GroupedBox model. I want to be able to store id for both of these schemas and to be able to populate this field accordingly. For instance, if the document will store an ObjectId that references a GroupedBox document, the populate method will fetch the right GroupedBox document. Same goes for a Box document id.
I read about Mixed field types but I don't see how that can help me.
replace box field in your code with this:
box: {
box: { type: Schema.Types.ObjectId, ref: 'Box' },
groupedBox : { type: Schema.Types.ObjectId, ref: 'GroupedBox' }
}
To populate box you need to access box with a dot populate("box.box") similar to groupedBox you populate it this way populate("box.groupedBox")

create a schema for nested objects in mongo db

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]

Resources