I have a simple schema with an array in there. When I create a new item including items in the mapping array, the items in the mapping array are automatically assigned an _id. However, when I try to add items to the mapping array of an existing item, the new mapping is created with an _id of null. How do I get mongoose to generate this _id for me? I can't find it anywhere in the documentation.
My schema is:
{
email: {
type: String,
required: true,
index: true,
},
mapping: [
{
mapToField: {
type: String,
enum: [
"subject",
"location",
"company",
"hours",
"rate",
"startDate",
"endDate",
],
required: true,
},
mapToLabel: {
type: String,
required: true,
},
regex: {
type: String,
required: true,
},
},
],
},
{ timestamps: true }
);
I have tried two ways to add an item to the mapping array but both options result in an item being added without an _id.
Option 1:
let item = await Mappings.findOne({ _id: id });
return await item.mapping.create(mapping);
Option 2:
return await Mappings.update(
{ _id: id },
{ $push: { mapping } },
{ upsert: true }
);
How do I get mongoose to generate an _id for the items in the mapping array?
Try defining mapping as an schema in your User schema file.
const mappingSchema = new mongoose.Schema({
mapToField: {
type: String,
enum: [
"subject",
"location",
"company",
"hours",
"rate",
"startDate",
"endDate",
],
required: true,
},
mapToLabel: {
type: String,
required: true,
},
regex: {
type: String,
required: true,
}
})
And then your main schema becomes
{
email: {
type: String,
required: true,
index: true
},
mappings: [mappingSchema]
}
Related
I want to generate two different objectIds in mongoose schema. One for departureLocation and one for arrivalLocation and want to reference it later.
I imported the object id from mongoose like this
let id = new mongoose.Types.ObjectId()
now I want to generate two different object ids like I said above
const routeSchema = new mongoose.Schema(
{
location: {
departureLocation: {
name: {
ref: "Location",
type: String,
required: true,
},
//want to create new object id here,
subLocation: [String],
_id: {
type: String,
},
},
arrivalLocation: {
name: {
ref: "Location",
type: String,
required: true,
},
//want to create new object id here,
subLocation: [String],
_id: {
type: String,
},
},
},
duration: {
type: Number,
required: true,
},
busId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Bus",
required: true,
},
date: {
type: String,
required: true,
},
},
{
timestamps: true,
}
);
MongoDB will automatically create _id for an object in a schema. You do not need to create one. In this schema, there will be 4 automatically generated _ids one for location, one for departureLocation, one for arrivalLocation, and one for the overall schema.
I am having a rough time querying my collection of data, Im building a simple E-Comm platform and in that platform i want to be able to have categories for the products. the idea is simple, each category should be its own object in the database that has a collection of products, however each product can have multiple categories, this is for products that might fit into one or more category definitions.
here is the model for the categories
import mongoose from "mongoose";
const categorySchema = mongoose.Schema(
{
cat_name: {
type: String,
required: true,
unique: true,
},
cat_items: [
{
product: {
type: mongoose.Schema.Types.ObjectId,
ref: "Product",
},
},
],
// Adds a relationship between a User admin and a product
// this is useful to see which admin, if multiple created a category
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "User",
},
},
{ timestamps: true }
);
const Category = mongoose.model("Category", categorySchema);
export default Category;
Here is the Product model
const productSchema = mongoose.Schema(
{
// Adds a relationship between a User admin and a product
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "User",
},
name: {
type: String,
required: true,
},
image: {
type: String,
required: true,
},
brand: {
type: String,
required: true,
},
category: {
type: String,
required: true,
},
categories: [
{
category: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category",
},
},
],
description: {
type: String,
required: true,
},
reviews: [reviewSchema],
rating: {
type: Number,
required: true,
default: 0,
},
numReviews: {
type: Number,
required: true,
default: 0,
},
price: {
type: Number,
required: true,
default: 0,
},
countInStock: {
type: Number,
required: true,
default: 0,
},
},
{
timestamps: true,
}
);
const Product = mongoose.model("Product", productSchema);
export default Product;
I believe the overall problem is the way i have the models setup, because any query i run on the categories object to return the products associated with it in cat_items, does not return any products it simply returns an empty array.
here is the simple function to return data
const products = await category.map((product)=> { return Product.findById(product._id)})
here im pulling out the array of category items, and mapping over them looking for the product in the database by the _id which is what product is example: 620e626203a59f0004e5a8c6 this in theory if you had 3 items, would return a new array of the product objects that i can send to the client, however every attempt only returns a [] and im pretty sure this is all do in part to the way i have the models setup.
for reference this is what returning a category looks like in postman:
{
"category": {
"_id": "62102f5c2b990d0e7c7d9bf8",
"cat_name": "Pendants",
"user": "620af3311fe4c247904b84d9",
"cat_items": [
{
"_id": "620e626203a59f0004e5a8c6"
},
{
"_id": "620e626203a59f0004e5a8c6"
}
],
"createdAt": "2022-02-18T23:44:28.697Z",
"updatedAt": "2022-02-18T23:54:23.103Z",
"__v": 2
},
"products": [
{},
{}
]
}
where im trying to fill the products array with the actual products stashed in the database
I'm new to mongoDB and backend stuffs. I've a nested MongoDB model schema. I need to update, delete and add in that nested objects/array. How can I do that ?
How can I delete/add/update item from columns array and resume array.
const userSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
},
columns: [
{
title: {
type: String,
required: true,
},
resumes: [
{
name: {
type: String,
required: true,
},
resume: {
type: String,
required: true,
},
},
],
},
],
});
const Users = mongoose.model('Users', userSchema);
This is my schema
To add new item to the columns array:
let newData = {title: "New Title", resumes: []}
let userData = await User.findByIdAndUpdate(userID, {
$push: {
columns: newData,
},
});
To add new item to the resumes array inside columns:
let newData = {name: "New Name", resume: "New Resume"}
let userData = await User.updateOne(
{
_id: mongoose.Types.ObjectId("60f54774761788cd80f56xxx"),
"columns._id": mongoose.Types.ObjectId("60f54774761788cd80f56xxx"),
},
{
$push: { "columns.$.resumes": newData },
}
);
Similarly, you can use $pull operator to remove an object from the array, and $(update) to update the element inside the array
I'm using mongoose. I want to select users property depending on another property at this here type.
for example when my type is private I want to select users.
Conversation.find({
users: {
$elemMatch: {
user: _id
}
}
},{
title: 1,
type: 1,
users:1 // when `type` is `private` I want to this field to be one.
});
my Schema:
const ConversationSchema = new Schema({
type: {type: String, enum: ['private', 'group'], default: 'private'},
creator: {type: Schema.Types.ObjectId, ref: 'User', index: true, required: true}, // creator
// for group,
title: String,
picture: String,
description: String,
users: [
{
user: { type: Schema.Types.ObjectId, index: true, reuqired: true, unique: true },
role: { type: String, enum: ['admin', 'member'], default: 'member' },
mute: { type: Boolean, default: false },
type: {type: String, enum: ['private', 'group'], default: 'private'},
}
],
}, { timestamps: true });
You can conditionally exclude fields by using REMOVE in aggregation. In your case, it should be:
Conversation.aggregate([
{$match: {"users.user": id}},
{
$project: {
title: 1,
type: 1,
users: {
$cond: {
if: { $eq: [ "$type", "private" ] },
then: "$users",
else: "$$REMOVE"
}
}
}
}
])
Side note: If you specify only a single condition in the $elemMatch expression, you do not need to use $elemMatch.
You can use aggregate like this, it will select all users if you want to users detail then will have to use populate.
db.getCollection("Conversation").aggregate([
{
$unwind: "$users"
},
{
$match: {
"type": 'private'
}
}
]);
Hi I have a Model Customer and its schema
var customerSchema = new Schema({
name: {type: String, required:true, trim: true, index: true, default: null, sparse: true},
cardInfo: { type: [cardInfoSchema]}
})
var cardInfoSchema = new Schema({
customerId : { type: String, required: true, index: true, unique: true,trim : true, sparse: true},
cards: { type:[cardsSchema], default:[]}
}, {_id: false});
var cardsSchema = new Schema({
cardType : { type: String, required: true, default: null, trim : true },
isActive : { type: String, required: true, trim: true }, //0: not active, 1: active
cardId : { type: String, required: true, trim: true },
createdAt : { type: Date, default: Date.now, required: true }
})
I want to add multiple cards of customer so I am using this
var dataToSet = {
'cardInfo.customerId': '25934285649875',
$push: {
'cardInfo.cards': {
cardId: 'somecardid,
cardType: 'type',
createdAt: new Date().toISOString(),
isActive: true
}
}
};
But I am getting this error.
cannot use the part (cardInfo of cardInfo.customerId) to traverse the element ({cardInfo: []})
Please help me in writing the query to add card of customer using mongodb or mongoose.
Try the positional $ operator in your update which acts as a placeholder for the first element that matches the query document, and when you use it make sure the cards array field must appear as part of the query document. In your case you'd want to add a card document into the 'cards' array, but only if the cardId doesn't exist:
var query = {
"cardInfo.cards.cardId": { "$nin": ["somecardid"] }
};
var update = {
"$push": {
"cardInfo": { "customerId": "25934285649875" },
"cardInfo.$.cards": {
cardId: "somecardid",
cardType: "type",
createdAt: new Date().toISOString(),
isActive: true
}
}
};
Customer.update(query, update, function (err, result) { ... });