In mongodb, which style is better? 1) or 2)? Can I retrieve only line name from 1) despite of getting whole record on db.record.find("line":"east")?
1.
{
"line": "east",
"station":[
{ "name": "ABC", "_id": "1", },
{ "name": "DEF", "_id": "2" }
]
}
2.
{ "line": "east", "l_id":"1"},
{"station":"ABC", "_id":"1", "l_id":"1"},
{"station":"ABC", "_id":"2", "l_id":"1"}
Note: line and station has one to many relationship.
If you are most commonly getting stations by line and often need all stations of a line alt 1 is the best. If you are commonly retrieving single stations or a subset of stations 2 is the best. Alt 2 can lead to more queries since mongo doesn't really have any relations, alt 1 can lead to larger reads and make it more difficult to keep the working set in RAM because of larger objects. Alt 1 also has a minor drawback if you change values in a station on multiple lines - then you have to update its instance in each of the line documents containg it.
To get a partial object, i.e. not all stations in alt 1, you can do a projection to filter out what you don't want. It still means reading the whole object into memory first so you wouldn't gain a lot in performance from doing that.
Related
How do I get random algolia item from it's index?
All of my items have:
objectID "POST#news#44.7704046#17.1900285"
name "News"
categories [ "cafe", "food", "establishment", "food" ]
_geoloc { lat: "44.7704046", lng: "17.1900285" }
I would like to optionally search by name, match 1 or all categories, geo location filtering with distance, and most importantly, I only want 1 RANDOM returned from Algolia.
I can't do client side random, because sometimes without filters I would get too many results back ( 10000 ), so I can't transfer that over the wire.
Please help
Hi #Djordje there are no real way to get a random result with Algolia though you you could use an attribute to randomise the results and only use the first item. See documentation [here][1]
Hey all i'm getting data from rest api everything is working fine but i want only text data. anyone pls help me.
{
"description": {
"blocks": [
{
"key": "1h2pe",
"text": "Delicious, loquats are very low in calories; provide just 47 cal per 100 g, however, rich in insoluble dietary fiber, pectin. Pectin retains moisture in the colon and thus functions as bulk laxative and by this way, it helps to protect the colon mucous membrane by decreasing exposure time to toxic substances as well as binding to cancer causing chemicals in the colon.Pectin has also been shown to reduce blood cholesterol levels by decreasing its re-absorption in the colon by binding bile acids resulting in its excretion from the body.Loquat fruit is an excellent source of vitamin-A (provides about 1528 IU per 100g), and phenolic flvonoid antioxidants such as chlorogenic acid, neo-chlorogenic acid, hydroxybenzoic acid, feruloylquinic acid, protocatechuic acid, epicatechin, coumaric acids and ferulic acid. Ripen fruits have more chlorogenic acid concentrations.Vitamin A maintains integrity of mucus membranes and skin. Lab studies have shown that consumption of natural fruits rich in vitamin-A and flavonoids helps to protect from lung and oral cavity cancers.Fresh fruit is very rich in potassium and some B-complex vitamins such as folates, vitamin B-6 and niacin and contain small amounts of vitamin-C. Potassium is an important component of cell and body fluids, helps controlling heart rate and blood pressure.It is also a good source of iron, copper, calcium, manganese, and other minerals. Manganese is used by the body as a co-factor for the antioxidant enzyme, superoxide dismutase. Copper is required in the production of red blood cells. Iron is required for as a cofactor in cellular oxidation as well for red blood cell formation.",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
}
],
"entityMap": {}
}
}
You need to create an Adapter class for whatever JSON you're trying to access in Dart.
In Java, they are called POJO classes.
Declare all the variables i.e., 'keys' as variables of type 'value' in a class, and use getters to access individual fields.
You can access the 'Text" data by...
void main() {
var mydata = {
"description": {
"blocks": [
{
"key": "1h2pe",
"text":
"Delicious, loquats are very low in calories; provide just 47 cal per 100 g, however, rich in insoluble dietary fiber, pectin. Pectin retains moisture in the colon and thus functions as bulk laxative and by this way, it helps to protect the colon mucous membrane by decreasing exposure time to toxic substances as well as binding to cancer causing chemicals in the colon.Pectin has also been shown to reduce blood cholesterol levels by decreasing its re-absorption in the colon by binding bile acids resulting in its excretion from the body.Loquat fruit is an excellent source of vitamin-A (provides about 1528 IU per 100g), and phenolic flvonoid antioxidants such as chlorogenic acid, neo-chlorogenic acid, hydroxybenzoic acid, feruloylquinic acid, protocatechuic acid, epicatechin, coumaric acids and ferulic acid. Ripen fruits have more chlorogenic acid concentrations.Vitamin A maintains integrity of mucus membranes and skin. Lab studies have shown that consumption of natural fruits rich in vitamin-A and flavonoids helps to protect from lung and oral cavity cancers.Fresh fruit is very rich in potassium and some B-complex vitamins such as folates, vitamin B-6 and niacin and contain small amounts of vitamin-C. Potassium is an important component of cell and body fluids, helps controlling heart rate and blood pressure.It is also a good source of iron, copper, calcium, manganese, and other minerals. Manganese is used by the body as a co-factor for the antioxidant enzyme, superoxide dismutase. Copper is required in the production of red blood cells. Iron is required for as a cofactor in cellular oxidation as well for red blood cell formation.",
"type": "unstyled",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [],
"data": {}
}
],
"entityMap": {}
}
};
List firstList = mydata.values.toList();
List secondList = firstList[0].values.toList();
print("${secondList[0][0]["text"]}");
}
To an array I want to update some elements and keep array's size no great than N. How to do it in an single operation?
Lets' make an example to make my question clear:
1)db.test.insertOne({a:[1, 100, 3, 5, 600]});
{
"_id" : ObjectId("5fc5a14e1b800790b21afa65"),
"a" : [
1.0,
100.0,
3.0,
5.0,
600.0
]
}
2) change it to
{
"_id" : ObjectId("5fc5a14e1b800790b21afa65"),
"a" : [
1.0,
100.0,
5.0
]
}
As shown above, a[2]->5, and N=3, only keeps a[0],a[1],a[2]. I have to make these happen in one single operation to make data consistent(don't consider multiple clients)
How could I do ? Thanks!
In your case, I'd recommend to follow this logic:
If the array in the memory has not been changed - do nothing.
If your array was changed and it's small then just update the whole array.
If your array was changed only a little bit and it's pretty big then calculate in your app what exactly elements were changed and create an update with corresponding operations for everyone (remove, update, insert). Be careful, it's an error-prone approach.
If your app runs into case number 3 "often", it means you designed a wrong data structure to solve your problem.
I have a collection dinosaurs with documents of this structure:
doc#1:
name: "Tyrannosaurus rex",
dominantColors: [
"beige",
"blue",
"green"
]
doc#2:
name: "Velociraptor",
dominantColors: [
"green",
"orange",
"white"
]
I want to query the collection by color name (for example: green) to get documents sorted by color's position in dominantColors array. First get the documents in which green occurs higher in the array, then those in which it is lower. So, in the provided case I would get doc#2 first, then doc#1.
Each dominantColors array contains 3 elements, with elements sorted from most dominant to least.
I am looking through documentation, but am not able to find a solution. Maybe I need a different data structure altogether?
Cloud Firestore doesn't support querying arrays by ranked index. The only way you can query an array is using an array-contains type query.
What you could do instead is organize your colors using maps where the color is the key and their rank is the value:
name: "Tyrannosaurus rex",
dominantColors: {
"beige": 1,
"blue": 2,
"green": 3
}
Then you can order the query by the value of the map's property. So, in JavaScript, it would be something like this:
firebase
.collection('dinosaurs')
.where('dominantColors.green', '>', 0)
.orderBy('dominantColors.green')
My goal is to have products that have some basic information like
Name
Description
Brand/Manufacturer
Dimensions & Weight
And optionally each product can have options based on
Size
Color
Material
I've read a few articles but couldn't find a suitable answer for my problem, which is how to reflect that all those possible combinations of options can have different SKUs, prices and amounts in stock.
And additionally I'd like to have different images for different colors of a product.
So my current thought is to have separate collections for all the options:
Size
Color
Material
Then have arrays of pointers for all those options within the product document and and additional array of variations which reflects every possible combination of options and adds a SKU, price and stock field.
{
_id: "12345",
name: "My Product",
...
colors: [
{
_id: "Color_1",
images: [
"http://myserver.com/images/product_12345_1",
"http://myserver.com/images/product_12345_2",
]
},
{
_id: "Color_3",
images: [
"http://myserver.com/images/product_12345_3",
"http://myserver.com/images/product_12345_4",
]
}
],
sizes: [
{
_id: "Size_5"
},
{
_id: "Size_9"
}
],
materials: [
{
_id: "Material_2"
}
],
variations: [
{
color: "Color_1",
size: "Size_5",
material: "Material_2",
SKU: "98765"
price: 10,
stock: 2
},
{
color: "Color_1",
size: "Size_9",
material: "Material_2",
SKU: "87654"
price: 11,
stock: 5
},
...
],
}
But somehow I feel that there might be an easier way to accomplish what I'm looking for.
Data modelling of Product information is more an art than a science.
It is very common to define Products as the entities sales thinks about. Let's say a car model or a cable. E.g. a "cat 5e Ethernet cable".
Such a product has attributes and dimensions. Attributes might be
standard / norm (e.g. EIA/TIA-586)
Manufacturer (Kabelwerk Eupen)
Number of Wires (8)
Packaging (Plastic Bag)
Tags (Network, Ethernet, Cabling, Infrastructure)
RHoS (Compliant)
etc.
Attributes tend to vary between industries and even between different product categories in the same company.
Dimensions distinguish between different variants of a Product. One or more Dimensions can define a concrete Product. Typical Dimensions are size and colour. For cables, we might have:
size / length (0.5, 1, 2, 5, 10 meters)
colour (green, red, blue)
flame retardant (yes, no)
So Products are the concept of one of your merchandise. In a paper catalogue a Product usually is described as a single thing (maybe on a single page). E.g. a jacket available in blue and brown in sizes S, M, L and XL.
What defines a single Product is somewhat fuzzy. The blue and green sneaker might be the same Product, but the orange and the golden might not be seen as the same product.
Before E-Commerce, we tended to expect the same price for all Dimensions of a Product - not long ago, people were scandalized if a size 8 shoe would be more expensive than a size 9 shoe.
Along some dimensions - colour mostly - users usually except pictures. Along other dimensions - e.g. shoe size - usually there is no expectation of specific pictures.
With some products the manufacturer might be considered an Dimension (cables), for others it might be considered irrelevant (cable ties) and for others two identical looking goods from different manufacturers might be considered completely different Products (e.g. sneakers).
The other concept are SKUs - the stock keeping units are the stuff which is actually in the warehouse. Usually per Product we have Dimensions multiplied with each other SKUs. So 5 sizes x 3 colours x 2 fire retardant variants - so there could be 30 SKUs. Usually each SKU has a distinct GTIN/UPC/EAN ("Barcode" 4423456789012).
Keeping SKUs and Products separate is best practice because they are related to different concerns: Products are of importance for marketing and sales. SKUs concern auditing, bookkeeping and logistics. Products represent concepts, SKUs represent physical goods. Amount of stock usually should be kept in or near the SKU - because on large commerce applications it might get updated several times per second. I would never design a system where transaction data - amount of stock - is mixed up with master data - product description, etc.
Pricing information has been historically attached to the product because product and price data is somewhat static but dynamic pricing might change that.
You seem to be asking for a Product Database. Schemaless Databases work nicely for this because it is very hard to anticipate all needed dimensions for the next few years. Normalizing the whole thing for a Relational Database can certainly be done, but tents to result in less than elegant, and slowish code.
{
name: "Cat 5e Cable",
…
dimensions: {
color: {
title: "Color",
red: {
title: "Red",
images: [
"http://myserver.com/images/product_12345_3",
"http://myserver.com/images/product_12345_4",
],
},
green: { … }
},
size: {
title: "Size"
s05: {
title: "0.5 m",
images: [],
},
s1: {...},
fireretardant:
title: "Size"
yes: {
title: "fire retardant",
images: [],
},
no: {
title: "not fire retardant",
images: [],
}
}
// here you need a stable way of generating keys from dimension data
variations: [
{
dimensions: {color: red, size: s1, fireretardant: no}
SKU: "98765"
price: 10,
},
{
dimensions: {color: red, size: s1, fireretardant: yes}
SKU: "98765"
price: 10,
},
},
…
]
I have implemented applications with such a schema. Usually you want to limit available dimensions and valid values per dimension in the Admin GUI so staff does not come up with obscure new dimensions all the time. But this should be an administrative restriction, not one of the underlying schema.
Non-existent combinations ("fire retardant is only available in green and not in 0.5 m), conflicting instructions ("make all 5 m cables 10 € and all red ones 8 €"), differing and inconsistent ideas what e.g. needs a image, what images should be shared between Dimensions, inconsistent definitions, what considered a separate product ("USB C) or just a Variant ("3.5 mm or 5.5 mm headphone jack"), translation and conversion (don't get me started with shoe sizes) makes real life database design and maintenance interesting …
this is what the so-called "domain knowledge" is about. You need to involve a shoe salesman to design a good shoe database.