I am a problem, which in my view, is very complicated. I wanted to see other thoughts about.
Created fields with the angular formly, these same fields are generated by a policy to be dealt with, the problem to record this data, I would need two repeats or not, still have not found a way out for not using 2 repeats, my code below is one more try unsuccessfully:
Repeat:
<md-card-content>
<h2 class="md-title">{{g.title}}</h2>
<div ng-repeat="f in g.fields and i in item.grupos.fields">
<field-create field="f" ng-model="i.model"></field-create>
<p>result: {{f | json}}</p>
</div>
</md-card-content>
</md-card>
If someone can be interested in helping me, I get more details from the rest of the code.
I hope ideas :)
UPDATED, NEW IDEIA, no sucess..
The problem is, I have recorded in my resource fields, which create fields dynamically. What I'm not getting is to record data in these fields.
a group of fields in the resource is created.
This field group is called in another view, a policy is whether the field is input, textarea or checkbox and renders the field.
When saving these fields have the same view are created other entries in the resource already with the data you enter in the fields.
Now my problem, I'm not able to show this data recorded in the view, because I have a repeat to check the recorded fields in the resource. I need another repeat to populate these fields.
I managed to explain as it is complicated.
i create Jsbin for help: http://jsbin.com/faquhizupo/1/edit?html,js,console,output
Your exact intent of showing up a large portion of code is not 100% clear. But I assume you are having difficulty in manipulating nested loops.
If you want to have nested `ng-repeat', use the below syntax.
JSON Object
$scope.myDataSet = [
{
"id" : 1234,
"desc" : "My Description",
"data": [
{
"sub_id" : "sub id 1",
"field1" : "Value 1",
"field2" : "Value 2"
},
{
"sub_id" : "sub id 2",
"field1" : "Value 3",
"field2" : "Value 4"
},
{
"sub_id" : "sub id 3",
"field1" : "Value 5",
"field2" : "Value 6"
}
]
},
{
"id" : 4567,
"desc" : "My Description2",
"data": [
{
"sub_id" : "sub id 3",
"field1" : "Value 1",
"field2" : "Value 2"
},
{
"sub_id" : "sub id 4",
"field1" : "Value 3",
"field2" : "Value 4"
},
{
"sub_id" : "sub id 5",
"field1" : "Value 5",
"field2" : "Value 6"
}
]
}
];
This can be displayed in HTML as below,
<div ng-repeat="data in myDataSet track by $index">
{{data.desc}}
<ul>
<li ng-repeat="record in data track by $index">
<div>
<h3>{record.sub_id}</h3>
{{record.field1}}
</div>
</li>
</ul>
</div>
If you want to access external loop items inside internal loop, that can be achieved as $parent.$index.
Related
This is a fairly generic issue, and after just getting started with MongoDB I am trying to find which is the better option for this schema design.
Suppose we have those entities: Product, Category and Order
In an SQL scenarion, this would be the traditional approach for the schema:
Category -> Product -> OrderDetails <- Order
(A Category has Many Products and and each Product has only A Category | A product is in many Orders and An order has Many products, therefore we create an OrderDetails join table)
How should the same approach be in a MongoDB Schema?
Suppose a category has at most 10 to 12 products only.
Scenario 1: Embedding Products into Categories:
Category:
{
"_id" : Category_ID,
"name" : "Category description"
"products" : [ { "product_name": "First product", "price": 500 }, { "product_name": "Second product", "price": 150 } ]
}
Order:
{
"_id" : Order_ID,
"order_total" : 500
"products" : [ { "product_name": "First product", "price": 500 }]
}
Scenario B: Embedding Category into Product AND having an arrays of product references in Order
Product
{
"_id" : First product,
"price" : 500
"category" : { "category_name": "First category name", "description": "some random description" }
}
Order
{
"_id" : Order_ID,
"order_total" : 500
"products" : [ product id1, product id2]
}
Depending on how many products each category has, embedding the product inside the category might be an ideal scenario (there's a 16MB limit on documents), and depending on the usage pattern, for example, do you always get a category and display the products inside of it? or is the usage pattern getting the product each time?
Another question to ask is, does the category need its own identity, do you have a page that lists more information about a category? Or is a category just a description? If that's the case then I'd personally treat it more as a tag, see the example below:
{
"_id" : id,
"name" : "Product name",
"description" : "Product name",
"price" : 500
"categories" : [ "Category name 1", "Category name 2" ]
}
As for the order document, I'd copy the whole product into the order, you always see similar patterns within say SQL, and this is because if the product changes like its price or name, you want the order to have a snapshot of the product at the time of the order. You don't want the price of the order to change after the customer has paid and it's been shipped.
{
"_id" : Order_ID,
// "order_total" : 500 * this would be calculated
"products" : [
{
"_id" : id,
"name" : "Product name 1",
"description" : "Product name 1",
"price" : 500
"categories" : [ "Category name 1", "Category name 2" ]
},
{
"_id" : id,
"name" : "Product name 2",
"description" : "Product name 2",
"price" : 500
"categories" : [ "Category name 1", "Category name 2" ]
}
]
}
I know this isn't an actual pure answer, more like a bunch more questions, however, I hope it gives some direction for your schema design.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
T =[{
"customername" : "A",
"type" : "software",
"orderhistory": [{
"items" : "item 1",
"price" : "20"
},
{
"items" : "item 2",
"price" : "10"
}
]
},
{ "customername" : "B",
"type" : "Accountant",
"orderhistory": [{
"items" : "item 1",
"price" : "20"
},
{
"items" : "item 2",
"price" : "10"
}
]
}
]
for item in T:
print("customername: {}\n type:{}\n orderhistory:{}\n" .format(item['customername'], item['type'],
item['orderhistory']))
T consists of two dictionaries which consist of customername, type, orderhistory(which is a list of dictionaries).
In the for loop, you are printing the contents of your dictionary T using format() function where {} means some element will be present there. Either you can represent is plainly as {} or you can number them as {0}, {1} , {2},.....
In the format(), all the elements you want to print in {} should be placed in order of printing.
But, you should change it to T[0]["customername"] to access the value associated with customername in first dict since it is a dict inside a list.
This program would firstly iterate through those 2 dictionaries that are present inside the list ( only because of the for loop). This means that the variable named item would take the following values sequentially:
Value of item first time
{
"customername" : "A",
"type" : "software",
"orderhistory": [{
"items" : "item 1",
"price" : "20"
},
{
"items" : "item 2",
"price" : "10"
}
]
}
Value of item the second time:
{ "customername" : "B",
"type" : "Accountant",
"orderhistory": [{
"items" : "item 1",
"price" : "20"
},
{
"items" : "item 2",
"price" : "10"
}
]
}
So whenever the variable named item gets these values, then the part of the code
print("customername: {}\n type:{}\n orderhistory:{}\n" .format(item['customername'], item['type'],
item['orderhistory']))
Gets executed. So to summarize, all the for loop does is to give the variable named item the above-mentioned values and then to run the print command every time variable item gets a new value.
You may learn about for loop in detail over here https://www.youtube.com/watch?v=0ZvaDa8eT5s
If you are not able to understand the print statement, then you may know more about it over here:
https://www.youtube.com/watch?v=fy10ci10R_g
"-Kj9Penv_LMRUIPSet0b" : {
"categories" : [ "food", "fashion"],
"contact" : "profile/contact/eieiiieie888x7ww28288_x22",
"location" : "New York, United States",
"name" : "Billybob Smith",
"social" : {
"twitter" : {
"followers" : "1,002",
"nickname" : "#billybob"
}
},
"state" : "0"
},
"eieiiieie888x7ww28288_x22" : {
"categories" : [ "food", "fashion" ],
"contact" : "profile/contact/eieiiieie888x7ww28288_x22",
"location" : "New York, United States",
"name" : "Billybob Smith.",
"social" : {
"twitter" : {
"followers" : "1,002",
"nickname" : "#billybob"
}
},
"socialID" : "twitter_id|558969977",
"state" : "0",
"uniqueID" : "eieiiieie888x7ww2828"
},
This is one .JSON example of a duplicate in my database. I have a lot of duplicates in my database. The only common piece of data I capture which uniquely identifies each user is their contact link. What is my best course of action to seek and remove duplicates from my database? I'm totally stuck. The second entry example is the more accurate and complete entry. Ideally, I could remove the first one and leave the second one behind.
Could really use some help here! Thank you so much!
I started playing with MongoDB recently and I created a small database for learning purposes.
I used this to get an array with items
> var myitems = db.items.find({ $or: [ {"title": "item 1"}, {"title": "item 2"}, {"title": "item 3"}] })
> myitems
{ "_id" : ObjectId("570a841a8b71efa49d08fdda"), "title" : "item 1", "date" : ISODate("2016-04-10T16:49:30.242Z") }
{ "_id" : ObjectId("570a850c8b71efa49d08fddb"), "title" : "item 2", "date" : ISODate("2016-04-10T16:53:32.554Z") }
{ "_id" : ObjectId("570a85128b71efa49d08fddc"), "title" : "item 3", "date" : ISODate("2016-04-10T16:53:38.554Z") }
I also have a list object
{
"_id": int,
"total_items": int,
"items": [
"item_id": int
]
}
I want to insert my items array into list collection entries. Something like this:
> db.lists.insert({ "total_items": 5, "items": { $addToSet: myitems} })
Of course it didn't work. And that's what I'm looking for. A way to get the IDs from my items list and use them to insert a new entry to lists.
How can I do that?
You're using the insert command when you should be using the update command.
db.lists.updateOne({
_id: "abc"
}, {
$addToSet: {
items: episodes
}
})
Full docs here.
Say you have a mongoDB collection called comments that contains documents like this:
{
"_id" : ObjectId("55fc4e8f2b21ff102bb20d94"),
"userName" : "user0053",
"comment" : [
{
"timestamp" : ISODate("2015-09-18T17:49:03.678Z"),
"title" : "Some title XYZ",
"status" : "Approved",
"content" : "blah blah blah"
},
{
"timestamp" : ISODate("2015-09-18T17:49:03.678Z"),
"title" : "Some nice looking title",
"status" : "Approved",
"content" : "blah blah blah"
},
{
"timestamp" : ISODate("2015-09-18T17:49:03.678Z"),
"title" : "A title",
"status" : "Spam",
"content" : "blah blah blah"
}
]
}
The comment element is an array of documents, each containing several elements, among those a status with 3 (or any) possible values, say Approved, Spam, PendingReview.
I want to query all the documents in the collection whose comment element contains an array in which ALL documents have a status of say "Approved".
In the example above, the document does not qualify because there is at least one comment element that has a status different to Approved.
I believe this cannot be done with the $all and $elemMatch options, but it would need to be done with an Aggregate pipeline. I just haven't been able to figure out how.
At it's simplest form, this can be achieved with $nin.
db.collection.find({"comment.status" : {$nin : ["Spam" , "PendingReview"]}})