I'm trying to add multiple compound unique index in mongoose schema, but only the first schema.index() is working independent if I change the order.
import * as mongoose from 'mongoose';
const Schema = mongoose.Schema;
const PrivilegeSchema = new mongoose.Schema({
application: {
type: Schema.Types.ObjectId,
required: true,
ref: 'Application',
autopopulate: true
},
section: {
type: String,
required: false
},
title: {
type: String,
required: false
},
permission: {
type: String,
required: true
},
active: {
type: Boolean,
required: true
}
});
PrivilegeSchema.index( { permission: 1, application: 1 }, { unique: true, partialFilterExpression: {active: true} } );
PrivilegeSchema.index({ section: 1, title: 1, application: 1 }, { unique: true, partialFilterExpression: {active: true} });
export { PrivilegeSchema };
How can I set both schema index
I found the issue, the existing data in the collection is restricting the index to be created. Hence the following validation is throwing.
I removed whole data from the collection and now both the index got created.
Related
I have a "cart" model, and within my "order" model, I would like to have an array that receives the information sent by "cart" stored in an array. How can I do this through ref?
const mongoose = require('mongoose');
const CartSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
note: {
type: String,
required: true,
},
price: {
type: Number,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Cart", CartSchema);
order
const mongoose = require('mongoose');
const OrderSchema = new mongoose.Schema(
{
list: {
name: String,
notes: String,
},
totalAmount: {
type: Number,
required: true,
},
payment: {
type: String,
required: true,
},
address: {
type: String,
required: true,
},
addressNote: {
type: String,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Order", OrderSchema);
Here in "list" I would like it to be an array that receives cart model information
Can I do this through ref? What would be the best possible way?
I have an order model/schema, and my goal is that in this model in "list", I receive the information from the model cart in array format. How could I do this using ref?
cart model
const mongoose = require('mongoose');
const CartSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
note: {
type: String,
required: true,
},
price: {
type: Number,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Cart", CartSchema);
order
const mongoose = require('mongoose');
const OrderSchema = new mongoose.Schema(
{
list: {
name: String,
notes: String,
},
totalAmount: {
type: Number,
required: true,
},
payment: {
type: String,
required: true,
},
address: {
type: String,
required: true,
},
addressNote: {
type: String,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Order", OrderSchema);
Basically receive in array format the information of the model cart in "list" in model order
You should define your list as an array of ObjectIds referring to the Cart model:
const OrderSchema = new mongoose.Schema(
{
list: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Cart'
}],
...
});
Then, to retrieve the values in list, just populate the Order:
Order.find({}).populate('list').exec();
I have the following Schema type called Orders. I am using Arrays of SchemaTypes in some properties. When I save it to the database, it's saving everything fine. I can open the database and see all the data there.
But the problem happens in one property called "files", whenever I try to use find() or findOne() or findById(), this property always comes empty, even if I have data to show.
This is my Schemas:
const statusChildSchema = new Mongoose.Schema({
...
});
const shippingChildSchema = new Mongoose.Schema({
...
});
const fileChildSchema = new Mongoose.Schema({
path: { type: String, required: true, trim: true },
type: { type: String, required: true },
});
const ordersSchema = new Mongoose.Schema(
{
// Relationships
creator: {
type: Mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Users',
autopopulate: false,
},
template: {
type: Mongoose.Schema.Types.ObjectId,
ref: 'Templates',
},
// Common
status: { type: String, trim: true, default: 'new', required: true },
// Child schemas
status_updates: [statusChildSchema],
shipping: [shippingChildSchema],
files: [fileChildSchema],
// Date properties
timings: {
created_at: { type: Date, default: Date.now, required: true },
...
},
},
{ collection: 'Orders', toJSON: { virtuals: true } }
);
Both statusChildSchema and shippingChildSchema is working normally. The problem is only with fileChildSchema. They are very similar, so I don't know what to do. I have researched in Mongoose documents and nothing helpful have been found.
This is the part of my code:
const order = await OrdersModel.findOne({
_id: orderId,
creator: userId,
});
console.log(order.files); // always printing "[]" empty array
I fixed it by installing last version of Mongoose (Version 5.11.8) and restarting everything. Looks like a bug.
I'm trying to define the following mongoose model for a case. Strange I can not see a proper documentation about what I'm trying to do. In the end I want to have only 1 collection "Case"
which documents are nested object like this:
const CaseSchema = new Schema({
clientDataType: {
type: String,
required: true,
enum: [
'dataSchema1',
'dataSchema2',
],
},
clientData: {
type: "Either dataSchema1 or dataSchema2",
required: true,
},
caseDataType: {
type: String,
required: true,
enum: ['type1', 'type2', ...]
},
caseData: {
type: "One of 6 types of cases again they are schemas",
required: true,
}
}, { timestamps: true });
And here is mockup for dataSchema1 same goes for dataSchema2
const dataSchema1 = new Schema({
field1: {
type: String,
required: true,
},
fiedl2: {
type: String,
required: true,
},
field3: {
type: "One of subschemas of my choice",
required: true,
},
...
}, { timestamps: true });
Is mongoose capable of doing that or I must skip mongoose for this kind of case model. Basically at the end I wan't to submit this object and let mongoose validate it for me.
Using the following Group Schema , will the role.name be unique IN the group only ? I would like to be able to store the same role name into another group button in the same group ...
/**
* Role Schema
*/
const Role = new mongoose.Schema({
name: { type: String, required: true, unique: true },
description: { type: String, required: false }
});
/**
* Group Schema
*/
const GroupSchema = new mongoose.Schema({
name: { type: String, index: { unique: true, required: true, dropDups: true } },
description: { type: String, required: false },
roles: [Role],
createdAt: {
type: Date,
default: Date.now
}
});