Get an array from a map property in mongoose - mongodb

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"]

Related

Create mongodb document with uuid named Object keys

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

Return objects with ids matching the ids in another array mongoose

I'm having an issue running a query on my mongo DB. I want to return the objects that match the ids in the array of Ids. Not sure what I'm doing wrong everywhere online suggests doing it this exact way.
Mongoose Query
MovieSchema.statics.findKnownForMovies = function(ids) { return this.find({ id: { $in: [ids]}});
}
Mongoose Schema
const MovieSchema = new Schema({
adult: { type: Boolean },
category: {type: String},
id: { type: Number, required: true, unique: true },
poster_path: { type: String },
overview: { type: String },
release_date: { type: String },
original_title: { type: String },
genre_ids: [{ type: Number }],
original_language: { type: String },
title: { type: String },
backdrop_path: { type: String },
popularity: { type: Number },
vote_count: { type: Number },
video: { type: Boolean },
vote_average: { type: Number })
Array of ids
[ '577922', '487558', '429203' ]

Updating array of objects in mongodb

I'm trying to update an array of objects in my simple-schema. Currently, it removes everything in the database and leaves behind:
"careerHistoryPositions": []
My simple-schema looks like:
const ProfileCandidateSchema = new SimpleSchema({
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
careerHistoryPositions: { type: Array, optional: true },
'careerHistoryPositions.$': { type: Object, optional: true },
'careerHistoryPositions.$.uniqueId': { type: String, optional: true },
'careerHistoryPositions.$.company': { type: String, optional: true },
'careerHistoryPositions.$.title': { type: String, optional: true }
});
If console.log form data looks like:
careerHistoryPositions: [Object, Object],
0: Object
company: "Test company"
title: "Test Title"
uniqueId: 1498004350350
1: Object
company: "Test company 2"
title: "Test Title 2"
uniqueId: 149800433221
My update function:
handleFormSubmit(event) {
event.preventDefault();
const { careerHistoryPositions } = this.state;
ProfileCandidate.update({ _id: this.state.profileCandidateCollectionId },
{ $set: {
careerHistoryPositions
}
}
);
}
I managed to fix this by mapping over my object and running 2 separate updates. The first removes the old element and the second adds the updated version. I'm sure there is a better way to do this, however, this does seem to work.
handleFormSubmit(event) {
event.preventDefault();
const { careerHistoryPositions } = this.state;
ProfileCandidate.update({_id: this.state.profileCandidateCollectionId}, { $unset: {
'careerHistoryPositions': {}
}
})
const updatePosition = this.state.careerHistoryPositions.map((position) => {
ProfileCandidate.update({_id: this.state.profileCandidateCollectionId}, { $push: {
'careerHistoryPositions': {
company: position.company,
title: position.title,
uniqueId: position.uniqueId
}
}
})

Meteor simple schema issue

I'm getting null array value in main_categories. My schema is for brand collection:
Schema Definition
Schema.main_category = new SimpleSchema({
name: {type: String},
icon_image: {type: String},
description: {type: String}
});
Main_Category.attachSchema(Schema.main_category);
Schema.brand = new SimpleSchema({
name: {
type: String,
},
admin_number: {
type: String,
},
company_name: {
type: String,
},
owner_name: {
type: String,
},
owner_number: {
type: String,
},
admin_comment: {
type: String,
},
address: {
type: Schema.address,
},
logo_image: {
type: String
},
staffs: {
type: Array
},
"staffs.$": {
type: Object
},
"staffs.$.type": {
type: String,
allowedValues: ['admin']
},
"staffs.$.user_id": {
type: String
},
main_categories: {
type: [Schema.main_category]
},
sub_categories: {
type: [Schema.sub_category]
},
showcase: {
type: Boolean
}
});
Brand.attachSchema(Schema.brand);
Implementation
"addBrandMethod": function(jsonData) {
var json = {
name: jsonData.brandName,
admin_number: jsonData.adminNumber,
company_name: jsonData.companyName,
address: jsonData.companyAddress,
owner_name: jsonData.ownerName,
owner_number: jsonData.ownerNumber,
admin_comment: "jsonData.adminComment",
logo_image: "fasdfa",
staffs: [{
type: "admin",
user_id: "jaskjjkj"
}],
main_categories: [{
"_id": "uBibwEqaoDkZtXhsR",
"name": "Hair",
"icon_image": "nbdenck",
"description": "Hair Cut with Massage"
}
],
sub_categories: Sub_Category.find().fetch(),
showcase: true
};
Brand.insert(json);
return "Success";
}
Try removing the _id key from the main_categories array.
You didn't specify the _id key in the schema and simple-schema will only add the key when it's a schema that's attached to a collection.
I was getting main_categories object null because main_categories file alphabetically down from brand schema file.. and in brand schema file i was getting object of main_categories schema undefined. when i paste file up to brand schema file then problem solve..

updating objects in double nested arrays in a collection

Having this schema:
Task = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue: function(){ return Random.id(); }
},
title: {
type: String,
label: "Task title",
}
});
Card = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue: function(){ return Random.id(); }
},
tasks: {
type: [Task],
label: "Card tasks",
optional: true
}
});
ProjectSchema = new SimpleSchema({
name: {
type: String,
label: "Project name"
},
cards: {
type: [Card],
label: "Project cards",
optional: true
}
});
I'm trying to update it with this function, passing the 3 ids and an object with the editted task:
editTask : function(projectId,cardId,taskId,oTask) {
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
check(projectId, String);
check(cardId, String);
check(taskId, String);
check(oTask, Object);
Projects.update({
_id: projectId,
cards: {
$elemMatch : {
_id: cardId,
tasks : {
_id : taskId
}
}
}
},
{
$set: {"tasks.$.Task": oTask}
});
}
Error says: when the modifier option is true, validation object must have at least one operator.
As far as I know that error means the $set: {"tasks.$.Task": oTask} it's not pointing correctly.
I've also tried: $set: {"cards.tasks.$.Task": oTask},$set: {"cards.$.tasks": oTask}