Reading the nested object in the nested object - javascript

My problem is reading properties of nested object, which is inside other nested object.
GraphQL
type Mapping {
id: ID!
partnerSegmentId: ID!
ctSegmentId: CtSegment!
}
type PartnerSegment {
id: ID!
name: String!
platformId: Int!
partner: Partner!
}
type Partner {
id: ID!
name: String!
}
Once I try to query it like:
{
allMappings {
partnerSegmentId {
id
name
partner {
id
}
}
}
}
I recieve:
{
"data": {
"allMappings": [
null
]
},
"errors": [
{
"message": "Cannot return null for non-nullable field Partner.name.",
"locations": [
{
"line": 8,
"column": 9
}
],
"path": [
"allMappings",
0,
"partnerSegmentId",
"partner",
"name"
]
}
]
}
Mapping schema
const mappingSchema = new mongoose.Schema(
{
partnerSegmentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'PartnerSegment',
required: [true, 'Mapping must have partner segment id.']
},
ctSegmentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'CtSegment',
required: [true, 'Mapping must have CT segment id.']
}
},
{ timestamps: true }
);
I tried to read separately Partner, PartnerSegment and Mapping models. All works fine. Any idea where i should search source of the problem? I've checked mongodb docs and ids looks okay. I suppose it's fault of my model.
If you would like to take a closer look it's project repo.

SOLUTION:
Garbage Id in the return value was caused by not working populate in the nested entity. The way I managed to solve the problem:
const allMappings = () =>
Mapping.find({})
.populate('user')
.populate('ctSegment')
.populate({
path: 'partnerSegment',
populate: {
path: 'partner'
}
})
.exec();

Related

Mongoose - Populating a nested array of objects not working

I have a collection called Orders that contains this schema:
const mongoose = require('mongoose');
const orderSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
restaurant: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Restaurant',
required: true
},
dishes: [
{
dish: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Dish'
},
amount: Number
}
],
price: {
type: Number,
required: true
},
comment: {
type: String,
required: false
},
status: {
type: String,
enum: ['PROCESSING', 'CANCELLED', 'COMPLETED', 'ERROR'],
default: 'PROCESSING'
},
timestamp: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('Order', orderSchema);
Inside my router, I have this code:
let orders = await Order.find({restaurant: restaurantID, status:'PROCESSING'}).populate('dishes._id').exec()
Order.find does not throw an exception, but it isnt working either.
I want the res.body to look like this:
{
"_id": "objectID",
"user": "objectID",
"restaurant": "objectID",
"dishes": [
{
"amount": number,
"dish": {
//dish object
}
},
...
],
//other order properties
},
...
]
But for some reason the dishes array looks like this:
"dishes": [
{
"amount": 1,
"_id": "6184e848e6d1974a0569783d"
}
],
What am I doing wrong?
I know that if populate() worked the res.body dishes array would not have a property called 'dish' and instead have a property called _id that would contain the dish object, but this shouldnt be hard to change once populate() works.
EDIT:
I realised that my createOrder route could be part of the problem since it ignores my schema and uses an id property for the objectID instead of dish. The array I save to the DB contains a property called id for the id instead of dish, but shouldnt my schema throw an exception when i try to save something like this to my database?
At first glance, I think the problem might be that you have a syntax problem.
Try
.populate('dishes').exec()
instead of
.populate('dishes._id').exec()

Mongoose - Update an object inside nested array

My Mongo schema looks like this,
I want to update a flashcard object located in an array of flashcard also located in an array of subject.
const classrooms = new mongoose.Schema({
name: String,
year: String,
student: [
{
firstname: String,
lastname: String,
mail: String,
userId: String,
}
],
subject: [
{
subjectId: String,
flashcard: [
{
title: String,
tag: []
}
]
}
]
});
What I am doing is
const flashcard = await classroomModel.findOneAndUpdate({
_id : classroomId,
"subject" : {
"subjectId" : subjectId,
"subject.flashcard" : {
"_id" : flashcardId
}
},
"$set" : {
"flashcard.title" : "new title"
}
})
But this is deleting all flashcards located inside an object.
Any help would be appreciated.
You need arrayFilters to specify the (to-be-updated) flashcard document that meets the criteria for subject and flashcard.
db.collection.update({
_id: 1//classroomId,
},
{
"$set": {
"subject.$[subject].flashcard.$[flashcard].title": "new title"
}
},
{
arrayFilters: [
{
"subject.subjectId": 1//subjectId
},
{
"flashcard._id": 1//flashcardId
}
]
})
Sample Mongo Playground
Although i accepted #yong shun which is the best approach, another way to do it in case you don't like mongoose syntax :
const classroom = await classroomModel.findOne(
{
_id: 1 //classroomId,
},
)
const subject = classroom.subject.find(
(currentSubject: any) => {
return currentSubject.subjectId == 1 //subjectId
}
)
const flashcard = subject.flashcard.find(
(currentFlashcard: any) => {
return currentFlashcard._id == 1 //flashcardId
}
)
flashcard.title = "my new title";
await classroomModel.updateOne(classroom);

Impossible to query nested mongoose array?

I want to Query and array with regex inside and mongoose (mongoDB) model.
I want to search inside the nested array of the Productmodel :
const productSchema = new schema(
{
name: requiredString,
sku: requiredUniqueNumber,
ean: requiredUniqueNumber,
suppliers: [{ type: mongoose.Schema.Types.ObjectId, ref: SupplierModel }],
categories: [{ type: mongoose.Schema.Types.ObjectId, ref: CategoryModel }],
mainImage: requiredString,
images: [{ type: String }],
description: requiredString,
stock: requiredNumber,
price: requiredNumber,
totalOrders: requiredNumber,
reviews: [review],
},
{
timestamps: true,
count: true,
}
);
The model inside the "suppliers" array is:
const supplierSchema = new schema(
{
supplierName: requiredUniqueString,
invoiceAddress: address,
visitAddress: address,
status: supplierStatusEnum,
contacts: address,
accountType: accountTypeEnum,
logo: requiredString,
user: { type: schema.Types.ObjectId, ref: "products" },
},
{
timestamps: true,
}
);
Now here's the problem, if i query and and populate() i get all the results. But for some reason I cannot search inside the Array containing several suppliers. Here's of what i have:
foundProducts = await ProductModel.find({
$or: [
{
name: {
$regex: regex,
},
},
{
"suppliers.supplierName": {
$regex: regex,
},
},
{
description: {
$regex: regex,
},
},
],
});
The object in JSON:
If he finds that the suppliers model contains the regex he should give back the whole porductmodel containing that supplier.
What is the best way to search in all the items inside of an nested array.
ps. I'm a junior developer comming from PostgreSQL, so bare with me while I'm trying this noSQL "thing" :)
I was doing the wrong query. I need to do
{
"suppliers._id": {
$regex: regex,
},
},
I can only search on _id, since this is the way that i "modeled" it.

Mongoose deep populate results in [Object] at 2nd level

After spent 1 day trying to get things done, I think it's time to ask for help.
I want to populate 2 levels of a document.
I tried with populate() but it seems to work only for first level, not deep populate! I read a lot at SO and I know it should work but I think I'm missing something really stupid...
Please let me know where I'm making mistakes.
Here are the relevant code.
Schemas
var compositionSchema = new Schema({
name: {
type: String,
required: true
},
contributions: [{
type: Schema.Types.ObjectId,
ref: 'Contribution'
}]
});
mongoose.model('Composition', compositionSchema);
var contributionSchema = new Schema({
name: {
type: String,
required: true
},
media: {
type: Schema.Types.ObjectId,
ref: 'Media'
}
});
mongoose.model('Contribution', contributionSchema);
var mediaSchema = new Schema({
name: {
type: String,
required: true
}
});
mongoose.model('Media', mediaSchema);
Actual documents saved in MongoDB
compositions:
{
"_id" : ObjectId("59e5db4595fe650a71fb0e07"),
"name" : "Test Pop 7",
"contributions" : [
ObjectId("59e5db4595fe650a71fb0e05")
]
}
contributions:
{
"_id" : ObjectId("59e5db4595fe650a71fb0e05"),
"name" : "Honda",
"media" : ObjectId("59e4ac5dacacd709eac2c856")
}
media:
{
"_id" : ObjectId("59e4ac5dacacd709eac2c856"),
"name" : "Logo_big.png",
"width" : 662,
"height" : 540
}
My tries (= the wrong code?)
In Node JS, when I do this (as per documentation):
Composition.findOne({ name: "Test Pop 7" })
.populate({
path: 'contributions',
model: 'Contribution',
populate: {
path: 'media',
model: 'Media',
}
})
.exec((error, doc) => {
if (error) { console.log(error); }
else {
console.log(doc);
}
});
prints out this, without actually populate the media field:
{ _id: 59e5db4595fe650a71fb0e07,
name: 'Test Pop 7',
contributions:
[ { _id: 59e5db4595fe650a71fb0e05,
name: 'Honda',
media: [Object] } ]
}
It works, keeping in mind the key-word in your question: prints. Printed, with console.log(), it just shows you the type (checked with typeof) of the document included in an array for some (2nd) level of nesting. If you do:
console.log(doc.contributions[0].media[0])
you will see your populated media document.

Error when using $addToSet and $each, Mongo and Node JS

I am trying to use $addToSet to add a array of Strings to a MongoDB.
The Object setup is as defined:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = mongoose.model('Company', {
License: { type: String, index: true },
"Classifications": [
{
Classification: { type: String, _id: false }
}
]
});
The current object looks like this in Mongo:
{
"_id": {
"$oid": "55b03f9337b46f10d38843d7"
},
"Classifications": [
"A - GENERAL ENGINEERING CONTRACTOR",
"B - GENERAL BUILDING CONTRACTOR",
"C12 - EARTHWORK AND PAVING"
],
"License": "CA_*****"
}
I am trying to run $addToSet on the following array:
var classifications = [ 'B - GENERAL BUILDING CONTRACTOR',
'C10 - ELECTRICAL',
'C33 - PAINTING AND DECORATING',
'C29 - MASONRY',
'C-9 - DRYWALL',
'C54 - TILE (CERAMIC AND MOSAIC)',
'C-5 - FRAMING AND ROUGH CARPENTRY',
'C20 - WARM-AIR HEATING, VENTILATING AND AIR-CONDITIONING',
'C36 - PLUMBING',
'C39 - ROOFING' ]
Company.update({ '_id': company._id }, { $addToSet: { "Classifications": { $each: classifications } } }
When I run Company.update in Node, I am getting the error
[TypeError: Cannot use 'in' operator to search for '_id' in C39 -
ROOFING]
Any idea why this is happening?
Finally figured out my issue.
In the Schema I had:
"Classifications": [
{
Classification: { type: String, _id: false }
}
]
Which was looking for a object with a String in it. The Schema should of just been:
"Classifications": [String]
Which allows for
Company.update({ '_id': company._id }, { $addToSet: { "Classifications": { $each: classifications } } }
to run and add the strings from the array with no issue

Categories