This is my schema:
{
_id: "FJwSEMdDriddXLKXh"
name: "t"
number: "5"
owners: [
{
_id: 1,
name: "Name",
address: "Address",
type: "Type",
gender: "Gender",
notes: []
}
]
}
and on click I would add fields inside owners nested notes array.
This is my Meteor template events:
Template.owners.event({
'click #addNoteToOwner' : function(event, template){
event.preventDefault();
Territories.update({_id: template.data._id, owners: this._id}, {$push : {'owners.$.notes': {title:"First Title"}}})
}
})
If I try to update the doc, the following console errorT appear:
Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]
Is my syntax correct? How can I update this nested array?
Thanks!
There is mistake in your query in owners field:
Territories.update({
_id: template.data._id,
owners: {
$elemMatch: {
_id: this._id
}
},
{
$push: {
'owners.$.notes': {
title: "First Title"
}
}
})
You won't be able to update directly from client side, because you use owners field and only _id is allowed. To solve this you can update using Meteor.methods and call that method from client side.
Related
I wasn't sure how to precisely word the title to describe my data structure, but it looks something like this.
[
{
_id: {
$oid: 231323123
},
OC: {
content: 'asdasfd',
date: '4314',
username: 'asdfasdfasf'
},
replies: [
{
_id: {
$oid: 12331234
},
OC: {
content: 'adfasdfadf',
date: '213124',
username: 'useruser'
},
replies: [
{
_id: {
$oid: 12331234
},
OC: {
content: 'adfasdfadf',
date: '213124',
username: 'useruser'
},
replies: []
}
]
},
{
_id: {
$oid: 3231231232
},
OC: {
content: 'asfasdf',
date: '1231232',
username: 'anotheruser'
},
replies: []
}
]
},
{
_id: {
$oid: 2331233
},
OC: {
content: 'mor econtent',
date: '122112',
username: 'okokok'
},
replies: []
}
]
This is just an example to get the point across, I'm trying to create a nested comment model but I don't know how to update any given replies array within any given nested comment using mongoose with mongoDB.
I was thinking I would do something like this:
CommentModel.updateOne({ _id: body.parentCommentId }, {
$push: {
`${body.referenceString}.replies`: {
new comment object
}
}
})
The problem is, I don't know how to automatically generate a reference string to get any nested comment array and update it with a new comment, or if this is even the correct approach at all. The position and contents of the nested comment would be decided by a front-end user and used to reference a back-end document within the database for update.
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.
Giving the following schema:
const Person = {
name: "User",
primaryKey: "_id",
properties: {
_id: "objectId",
name: "string",
age: "number",
company: "Company[]"
}
};
const Company = {
name: "Company",
primaryKey: "_id",
properties: {
_id: "objectId",
name: "string",
boss: {
type: 'linkingObjects',
objectType: 'Person',
property: 'company'
}
}
};
After adding some persons and several of them with a company, I try to filter by age and get the ones that are over 30 years with the code realm.objects("Person").filtered("age > 30");
The result of this query is the following error:
Maximum call stack size exceeded.
I expect to have a list of Persons which meets the requirement, but instead I have this error. I have seen that in the release 10.0.0 this error is suppose to be fix for toJSON() function. If I try to filter the object by its id everything works fine, but once I try to get a list of some objects I get this error.
Does somebody have a solution for this?
I´m using -->
Realm: 10.1.1
React Native: 0.62.2
Node: 10.22.1
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();
I have been trying to create two collection with a common model kind. I am getting the following error:
"Uncaught enyo.Store.addRecord: duplicate record added to store for kind app.ImageModel with primaryKey set to id and the same value of 67774271 which cannot coexist for the kind without the ignoreDuplicates flag of the store set to true ".
Following are the two collection i have defined...
enyo.kind({
name: "app.FeatureCollection",
kind: "enyo.Collection",
model: "app.ImageModel",
defaultSource: "appF",
...
...
});
enyo.kind({
name: "app.SearchCollection",
kind: "enyo.Collection",
model: "app.ImageModel",
defaultSource: "appS",
...
...
});
And the model which i am using is as follows:
enyo.kind({
name: "app.ImageModel",
kind: "enyo.Model",
readOnly: true,
....
....
});
At one point i am setting like this:
this.set("data", new app.FeatureCollection());
and in another,
this.set("data", new app.SearchCollection());
I am not able to find out what could generate the error. I even tried to set "ignoreDuplicates" to true in model...but still the error comes. Any suggestion where i could be going wrong.
The ignoreDuplicates flag is expected to be set on enyo.Store and not enyo.Model:
enyo.store.ignoreDuplicates = true;
Are you using the fetch method of enyo.Collection to retrieve your data? If so, you might consider setting the strategy property to merge in your fetch call so that you have a single record for each unique image from your dataset, i.e.:
myCollection.fetch({strategy: "merge", success: function(rec, opts, res) {
// do something after data is retrieved
}});
I'm not seeing a problem with the pieces of code you provided. I created a sample on jsFiddle and it works as expected.
http://jsfiddle.net/z7WwZ/
Maybe the issue is in some other part of your code?
enyo.kind({
name: "app.FeatureCollection",
kind: "enyo.Collection",
model: "app.MyModel"
});
enyo.kind({
name: "app.SearchCollection",
kind: "enyo.Collection",
model: "app.MyModel"
});
enyo.kind({
name: "app.MyModel",
kind: "enyo.Model",
readOnly: true,
defaults: {
firstName: "Unknown",
lastName: "Unknown"
}
});
enyo.kind({
name: "App",
components: [],
bindings: [],
create: enyo.inherit(function (sup) {
return function () {
sup.apply(this, arguments);
this.collection1 = new app.FeatureCollection(this.data1);
enyo.log("Collection1(0) >>> " + this.collection1.at(0).get("lastName"));
this.collection1.at(0).set("lastName", "Smith");
enyo.log("Collection1(0) >>> " + this.collection1.at(0).get("lastName"));
this.collection2 = new app.SearchCollection(this.data2);
enyo.log("Collection2(0) >>> " + this.collection2.at(0).get("lastName"));
this.collection1.at(0).set("lastName", "Jones");
enyo.log("Collection2(0) >>> " + this.collection1.at(0).get("lastName"));
};
}),
data1: [{
firstName: "Hall",
lastName: "Caldwell"
}, {
firstName: "Felicia",
lastName: "Fitzpatrick"
}, {
firstName: "Delgado",
lastName: "Cole"
}],
data2: [{
firstName: "Alejandra",
lastName: "Walsh"
}, {
firstName: "Marquez",
lastName: "James"
}, {
firstName: "Barr",
lastName: "Lott"
}]
});
new App().renderInto(document.body);