I want to create a document like shown below in mongoose. There is a pageElements Object with uuid Object keys inside.
{
"pageElements": {
"fc6b151-cd5-3c1-0e00-241b4411d4eb": {
"containers": {
"de325a-acb2-e0a1-e521-20b3e81e33f": {
"components": [{
...
}],
}
},
},
"075f7c-5aff-c850-a731-a8dff1bea5f8": {
"containers": {
...
}
}
},
}
I tried to create the Schema like this:
const componentSchema = new mongoose.Schema(
{
...
},
);
const containersSchema = new mongoose.Schema(
{
components: [{ type: componentSchema, required: true }],
},
);
const pageElementsSchema = new mongoose.Schema(
{
containers: { type: containersSchema, required: true },
},
);
const pageSchema = new mongoose.Schema(
{
pageElements: {
type: pageElementsSchema,
},
},
);
But the new created document only contains an empty pageElements Object.
Use Map as
const componentSchema = new mongoose.Schema(
{
...
},
);
const containersMapSchema = new mongoose.Schema(
{
components: [{ type: componentSchema, required: true }],
},
);
const pageElementsMapSchema = new mongoose.Schema({
containers: {
type: Map,
of: containersMapSchema
}
})
const pageSchema = new mongoose.Schema(
{
pageElements: {
type: Map,
of: pageElementsMapSchema
},
},
);
Related
i have json data like below:
{"ModelId":"m1","AddCompleted":[{"UniqueId":"c1"}, {"UniqueId":"c2"}],"AddActive":[{"UniqueId":"a1"}],"AddPlanned":[{"UniqueId":"p1"}]}
do i have to use nested/sub-schema like below:
const mySchema = new mongoose.Schema({
ModelId: { type: string, unique: true, required: true },
AddCompleted: [ new Schema({ UniqueId: string }), { _id: false } ],
AddActive: [ new Schema({ UniqueId: string }), { _id: false } ],
AddPlanned: [ new Schema({ UniqueId: string }, { _id: false }) ],
});
or can simply use:
const mySchema = new mongoose.Schema({
ModelId: { type: string, unique: true, required: true },
AddCompleted: [ { type: string } ],
AddActive: [ { type: string } ],
AddPlanned: [ { type: string } ],
});
my model schema look like this
const mongoose = require("mongoose")
const userSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
select: false,
},
email: {
type: String,
required: true,
unique: true,
match: [
/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/,
"Please enter a valid email",
],
},
followers:[
{
type:mongoose.Schema.Types.ObjectId,
ref:"user"
}
],
following:[
{
type:mongoose.Schema.Types.ObjectId,
ref:"user"
}
],
displayName: {
type: String,
required: false,
},
},
{ timestamps: true }
)
module.exports = mongoose.model("user", userSchema)
in this schema all working good like mutation work fine but when i fetch query of all user then in that query followers and following field return null like bellow image
and my graphql query is
const users = {
type: new GraphQLList(UserType),
description: "Retrieves list of users",
resolve(parent, args) {
return User.find()
},
}
and typedef is
const UserType = new GraphQLObjectType({
name: "User",
description: "User types",
fields: () => ({
id: { type: GraphQLID },
username: { type: GraphQLString },
email: { type: GraphQLString },
post:{
type: GraphQLList(PostType),
resolve(parent, args) {
return Post.find({ authorId: parent.id })
},
},
savePost:{
type:GraphQLList(savedPosts1),
resolve(parent, args) {
return SavePost.findById({ authorId: parent.id })
},
},
followers:{
type:GraphQLList(UserType),
},
following:{
type:GraphQLList(UserType)
}
// displayName: { type: GraphQLString },
}),
})
so please tell me how to i resolve that followers and following query in graphql with mongodb and tell me what i write in my userType typedef
Here's my Mongoose model schema:
MyBox = new Schema(
{
id: { type: Schema.Types.ObjectId },
name: { type: Schema.Types.String},
value: { type: Schema.Types.Number},
};
MyCar = new Schema(
{
id: { type: Schema.Types.Map,ObjectId },
box: { type: Schema.Types.Map, of: MyBox}
};
Data example
{ "_id" : 1,
"box" : {
"box_for_angel": { name: "apple", price:100 },
"box_for_peter": { name: "banana", price:120 }
}
}
how could I get the name of box map?
like ["apple", "banana"]
How do I delete object from inner schema in mongoose?
I try to delete comments from the Holiday Schema, this is the holiday schema:
const holidaySchema = new mongoose.Schema(
{
comments: [commentSchema],
},
)
const Holiday = mongoose.model("Holiday", holidaySchema);
export default Holiday;
and this is the comments schema:
const commentSchema = new mongoose.Schema(
{
action: { type: String },
time: { type: String },
name: { type: String },
image: { type: String },
content: { type: String },
rating: { type: Number },
},
{
timestamps: true,
}
);
I try to delete a specific comment from the holidaySchema in this way:
holidayRouter.delete(
"/:id/comments/:commentId",
isAuth,
expressAsyncHandler(async (req, res) => {
const holiday = await Holiday.updateOne(
{ _id: req.params.id },
{ $pull: { comments: { _id: req.params.commentId } } }
);
if(holiday){
console.log(holiday);
}
})
);
the console:
and this is not working, do you know what I am doing wrong or what should I do?
thank you
Mongoose converts the object into json, and we can customize that json which is returned.
commentSchema.methods.toJSON = function(){
const commentSchema = this.toObject()
delete commentSchema.name
delete commentSchema.rating
return commentSchema
}
New the JSON which is returned will not have name and rating.
I need to update different fields of a nested array in Mongoose. Sometimes I will send runId and runStatus, some other times siteFetched and some other times siteInfo.
I have tried with the following code but the $set operator replaces the old fields.
The model:
campaignId: { type: String },
keywords: [{
keyword: { type: String },
serp: {
runId: { type: String },
runStatus: { type: String },
siteFetched: { type: Boolean },
sitesInfo: [{
title: { type: String },
url: { type: String },
description: { type: String },
}],
},
},
],
Here is the code to update
const campaign = await Campaign.findOneAndUpdate(
{ _id: campaignId, "keywords.keyword": keyword },
{
$set: { "keywords.$.apifySerp": {...serp }},
}
);
the value for serp varies like
const serp = {
runId: '1kLgbnvpADsDJyP1x',
runStatus: 'READY'
}
and
const serp = {
siteFetched: true
}
Here is the code that solved my problem.
const serp = {
siteFetched: true,
};
let update = Object.keys(serp).reduce((acc, cur) => {
acc[`keywords.$.apifySerp.${cur}`] = serp[cur];
return acc;
}, {});