How can i handle this scenario - backbone.js

I am using Backbone.js, unexpectedly i am in the confused position now, because of this scenario..
my test object :
test: {
"links": {
"nameLink": {
"name": "name 1",
"name": "name 2",
"name": "name 3"
},
"ageLink": {
"age": "1",
"age": "2",
"age": "3"
}
},
"data": {
"element": {
"project": "project 1",
"title": "title1",
"brand": "brand1"
},
"element": {
"project": "project 2",
"title": "title2",
"brand": "brand2"
},
"element": {
"project": "project 3",
"title": "title3",
"brand": "brand3"
}
}
}
I do have the josn data from server, it has the project name, links(there is nested links under), time stamps.
how can i split the data, and append appropriate UL elements. ( by name, by age ect.)
On click on the name, age links, how can sort (by name, age) the datas i appended to body tag? - without sorting the links menu?
How can i keep all this updated?
any good suggestion please?

Related

In Azure Search how can I filter records that have tags with IDs matching any in a list

Given the following search index document schema:
{
"value": [
{
"Id": "abc",
"Name": "Some name",
"Tags": [
{
"Id": "123",
"Name": "Tag123"
},
{
"Id": "456",
"Name": "Tag456"
}
]
},
{
"Id": "xyz",
"Name": "Some name",
"Tags": [
{
"Id": "123",
"Name": "Tag123"
},
{
"Id": "789",
"Name": "Tag789"
}
]
},
]
}
What is the correct syntax for an OData query that will return all records with any Tag/Ids that are contained in input list?
The closest I have got is:
Tags/any(object: object/Id search.in ('123,456,789'))

How to get a value in one mongodb collection and use that value to update another document in another collection order & inventory system

Hello I have been stuck for weeks trying to figure how to create a order & inventory system for a project I am working on. I don't know how to properly ask this but my problem is when a user adds items to their cart > I store the order details in a orders collection in mongodb > I then need to figure out how to subtract the quantity of the items in a customers order from my inventory collection. How can I do this with mongodb, Python
This is the document created when a customer places an order
{
"_id": "5eca94b4f56331fd9eade681",
"ordernumber": 343,
"order": {
"order_details": [
{
"customer_info": [
{
"first_name": "John",
"last_name": "Doe",
"email": "email#email.com"
}
],
"shipping_details": [
{
"shipping_address": "Test Address",
"shipping_zip": "12345",
"shippingl_city": "Test city",
"shipping_country": "USA"
}
],
"products_ordered": [
{
"variant_id": "a",
"product_name": "red shirt",
"price": 30,
"quantity": 2,
"image": "imageurl",
"size": "Small"
},
{
"variant_id": "f",
"product_name": "Blue Jeans",
"price": 20,
"quantity": 3,
"image": "imageurl",
"size": "Large"
}
]
}
]
}
}
These are the products in my inventory collection I want inventory order quantity subtracted by the quantity a customer purchased
{
"_id": "5eca0ff4898b8f30a9fee5e5",
"product_id": 1,
"product_name": "red shirt",
"category": "shirts",
"price": 30,
"status": "instock",
"description": "nice red shirt",
"alt": "string",
"images": [
"imageUrl1",
"imageUrl2"
],
"variants": [
{
"Small": [
{
"variant_id": "a",
"inventory": 30
}
],
"Medium": [
{
"variant_id": "b",
"inventory": 10
}
],
"Large": [
{
"variant_id": "c",
"inventory": 10
}
]
}
]
}
{
"_id": "5eca108f898b8f30a9fee5e6",
"product_id": 2,
"product_name": "blue jeans",
"category": "jeans",
"price": 20,
"status": "instock",
"description": "nice blue jeans",
"alt": "string",
"images": [
"ImageURL"
],
"variants": [
{
"Small": [
{
"variant_id": "d",
"inventory": 100
}
],
"Medium": [
{
"variant_id": "e",
"inventory": 150
}
],
"Large": [
{
"variant_id": "f",
"inventory": 70
}
] }
]
}
I would suggest to do it along with the service which creates the order.
I would also like to suggest to refactor the db structure a bit as it would be harder to maintain this in a larger scale.
Because currently we would have to write something like
for ordered_product in products_ordered:
query = { "product_name": ordered_product.get("product_name") }
inventory_product = inventory_collection.find_one(query)
product_id = inventory_product["_id"]
existing_count = inventory_product["variants"][0][ordered_product.size][0]["inventory"]
inventory_product["variants"][0][ordered_product["size"]][0]["inventory"] = existing_count - ordered_product["quantity"]
inventory_collection.update_one({ "_id": product_id }, { "$set": inventory_product })
I have hardcoded the index values of the list. You could use filter() to filter out the variant and size you need.
This code definitely seems messy to me.
Of course you could refactor this code by splitting it into functions inside the model file itself, but I would suggest to refactor the db structure for better scalability.
May be you could move the variants to a seperate collection and use the product_id as a link. You have to think this through before getting on with the code.
Hope this helps.

How do I read a nested structure from solr?

I am lost and need your advice. I have a nested Solr Document containing multiple levels of sub-documents. Here is a JSON example so you can see the full structure:
{
"id": "Test Library",
"description": "example of nested document",
"content_type": "library",
"authors": [{
"id": "author1",
"content_type": "author",
"name": "First Author",
"books": [{
"id": "book1",
"content_type": "book",
"title": "title of book 1"
}],
"shortStories": [{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
}]
},
{
"id": "author2",
"content_type": "author",
"name": "Second Author",
"books": [{
"id": "book1",
"content_type": "book",
"title": "title of book 1"
}],
"shortStories": [{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
}]
}]
}
I want to query for a document and retrieve the nested structure. I tried using the ChildDocumentTranformerFactory but it flattened the result to be just Library and all other documents as children:
{
"id": "Test Library",
"description": "example of nested document",
"content_type": "library",
"_childDocuments_":[
{"id": "author1",
"content_type": "author",
"name": "First Author"
},
{"id": "book1",
"content_type": "book",
"title": "title of book 1"
},
{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
},
{
"id": "author2",
"content_type": "author",
"name": "Second Author"
},
{
"id": "book1",
"content_type": "book",
"title": "title of book 1"
},
{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
}
]
}
Here are the query parameters I used:
q={!parent which='content_type:library'}
df=id
fl=*,[child parentFilter='content_type:library' childFilter='id:*']
wt=json
indent=true
What is the best way to read the nested structure from Solr? Do I need to do some sort of faceting?
I am using Solr version 5.2.1
Unfortunately solr does not support that right now.
ChildDocTransformerFactory returns all descendant documents of each parent document matching your query in a flat list nested inside the matching parent document
Not sure if there is a work around for this.
Please look into Block Join to virtually relate the parent and child.
https://github.com/lucidworks/solrj-nested-docs

How to retrieve data from two objects in json file?

If I have records for users and also records for orders, it is proper to save them like that:
{
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "item": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]
}
as you can see I'm coming from the SQL world so I'm saving the 'orders' data with foreign key, is that the right way to save this data as json and if so how can I get all the users with there orders?
Thanks!
If your json object is in the above mentioned structure you can use this code to filter the orders
var oJson = {
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "i`enter code here`tem": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]}
for(var i=0; i<oJson.users.length;i++){
for(var j=0; j<oJson.orders.length;j++){
if (oJson.users[i].id == oJson.orders[j].userId) {
console.log( oJson.orders[j].item, oJson.orders[j].userId)
//your logic here
}
}
}
But it is better to use sql joints to merge the orders and return the objects like this
{
"users": [
{"id": "1", "name": "Allan", "age": "40","orders": [{ "id": "1", "item": "item 1", "userId": "1"},{ "id": "2","item": "item 3", "userId": "1"},{ "id": "1", "item": "item 4", "userId": "1"},{ "id": "1", "item": "item 51", "userId": "1"},{ "id": "2", "item": "item 4", "userId": "1"}
]},
{"id": "2", "name": "Jack", "age": "50","orders":[]}
]}
Fetch the objects for json file into two varible
var a=value.users;
var b=value.orders;
value is nothing but the whole json file
then you can match them against each other like
a[0].id==b.[0].id

Sorting arrays in Mongo

H have json document with array. As I can't add to beginning of array with push or addtoset I need to sort array. Example
{
"Component": [
{
"Id": "PDP-1",
"Links": {"Link": [
{
"Text": "Western Division",
"Url": "/1x7-en70ai/last-minute-holidays-western-division",
"Title": "Last minute holidays Western Division"
},
{
"Text": "Browse Regions ",
"Url": "/1x7-en6uly-10ts/last-minute-holidays-gambia/regions",
"Title": "Last minute holidays Gambia",
"Style": "BrowseForMore"
},
{
"Text": "City of Banjul",
"Url": "/1x6-en6vq7/holidays-city-of-banjul",
"Title": "City of Banjul Holidays"
},
{
"Text": "Western Division",
"Url": "/1x6-en70ai/holidays-western-division",
"Title": "Western Division Holidays"
}
]},
"Title": "Regions",
"Type": "PDP"
},
{
"Id": "PDP-2",
"Links": {"Link": [
{
"Text": "Bijilo",
"Url": "/1x7-enbmy6/last-minute-holidays-bijilo",
"Title": "Last minute holidays Bijilo"
},
{
"Text": "Browse Cities ",
"Url": "/1x7-en6uly-10tt/last-minute-holidays-gambia/cities",
"Title": "Last minute holidays Gambia",
"Style": "BrowseForMore"
},
{
"Text": "Banjul Beach",
"Url": "/1x6-enakgm/holidays-banjul-beach",
"Title": "Banjul Beach Holidays"
},
{
"Text": "Bijilo",
"Url": "/1x6-enbmy6/holidays-bijilo",
"Title": "Bijilo Holidays"
},
{
"Text": "Brufut Heights",
"Url": "/1x6-encok8/holidays-brufut-heights",
"Title": "Brufut Heights Holidays"
},
{
"Text": "Kololi",
"Url": "/1x6-enpnle/holidays-kololi",
"Title": "Kololi Holidays"
},
{
"Text": "Kotu",
"Url": "/1x6-enq067/holidays-kotu",
"Title": "Kotu Holidays"
}
]},
"Title": "Cities",
"Type": "PDP"
}
],
"Id": "118431",
"Template": {
"PageTemplate": {
"Code": "2B2",
"text": "041 - TEMP2 - COP_CONCOU_{LAST MINUTE}"
},
"Category": {
"Code": "1X7",
"Type": "Product",
"text": "Last minute holidays"
},
"GeoObject": {
"Code": "EN6ULY",
"text": "Gambia, The"
},
"GeoObjectType": {
"Code": "1A",
"text": "Political"
},
"GeoObjectSubType": {
"Code": "10TR",
"text": "Country"
}
},
"Type": "Content",
"Url": "/1x7-en6uly/last-minute-holidays-gambia",
"_id": {"$oid": "528492d4c90fa9fcd0436929"}
}
I want to sort this by Style in Links.Link 'BrowseForMore'. Any idea how to do it? I thought I could add dummy array with push which could then sort it the way I want. Any help appreciated
You appear to want to update the array and keep the sort order with your Links.Link.Style value at the front of the list. In which case use the $sort modifier with update.
db.collection.update(
{ _id: id },
{ $push: { "Links.Link: {$each: [doc], $sort { Style: -1 }}} }
)
The $each operator is required even if there is only one document, but can take many.
if you are trying to use $addToSet to maintain unique documents the official MongoDB line is that sets are considered to be unordered and hence the modifiers are not available here.

Resources