Mongodb multilingual query - mongodb

Material collection represents user submissions.
Default language is defined by the user. Other users can extend first submission with some other languages.
When I request a material, maybe I need more than one language, so I think it fits ok with just one collection where I should support multilanguage.
My current desing is like this (document for testing):
{
"activities": [ "Ficha de actividades", "Juego Colectivo" ],
"files": [ "Asociacion_Profesiones.zip"],
"areas": ["Literatura", "Ciencias Naturales", "Ciencias Sociales" ],
"authors": [
{ "author": "César", "email": "xxx#gmail.com"},
{ "author": "José", "email": "xxxx#gmail.com"}
],
"desc": "La leche, el agua, el vino y la bebida",
"state": 1,
"date": ISODate("2017-01-10 19:40:39"),
"updated": null,
"id": 1,
"language": "spanish",
"images": [],
"license": "Creative Commons BY-NC-SA",
"downloads": 0,
"popular": false,
"title": "La bebida",
"file": "1.zip",
"translations": [
{"language": "english", "title": "Beverages", "desc": "Milk, water, wine and all related with drinking", "authors": [], "files": [], "file": "1.zip", "images": [], "downloads": 0, "state": 1, "created": null, "updated": null},
{"language": "french", "title": "le boisson", "desc": "du lait, de l'eau, du vin, boire", "authors": [], "files": [], "file": "1.zip", "images": [], "downloads": 0, "state": 1, "created": null, "updated": null}
]
}
I create a text index focused on title and descripcion (desc) fields.
db.materials.createIndex({ "title": "text", "desc": "text", "translations.title": "text", "translations.desc": "text"})
I know the user default language, but I don't know how he should make the search.
Imagine an spanish user want to search for drinks:
db.materials.find( { $text: { $search: "drink", $language: "es"} } ).explain(true)
It returns our document. It searches drink, and as "Milk, water, wine and all related with drinking" has drink as stemming for drinking it works ok.
However... if I look for water:
db.materials.find( { $text: { $search: "water", $language: "es"} } ).explain(true)
It returns nothing. water gets converted to wat (as spanish verbs end with er suffix) and "wat" is not found.
Any improvement to my searches?
The only workaround I can imagine is to add language dropdown selector to user searches.

Related

Shopify REST API: Unable to add a new product variant with new option type getting error "Option values provided for 1 unknown option(s)"

when creating a product in Shopify via the REST API, adds a default option and variant like below
"options": [
{
"id": 9651869188247,
"product_id": 7644173172887,
"name": "Title",
"position": 1,
"values": [
"Default Title"
]
}
]
Variants:
{
"variants": [
{
"id": 42211487744151,
"product_id": 7644173172887,
"title": "Default Title",
"price": "1.00",
"sku": "",
"position": 1,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "Default Title",
"option2": null,
"option3": null,
"created_at": "2022-06-17T17:18:24+05:30",
"updated_at": "2022-06-17T17:18:24+05:30",
"taxable": true,
"barcode": null,
"grams": 0,
"image_id": null,
"weight": 0.0,
"weight_unit": "kg",
"inventory_item_id": 44310083534999,
"inventory_quantity": 0,
"old_inventory_quantity": 0,
"requires_shipping": true,
"admin_graphql_api_id": "gid://shopify/ProductVariant/42211487744151"
}
]
}
I want to update the existing Option and Variants of a product to show "Color" & "Size" as options and variants of them. For that I tried the Shopify Rest API but that still is raising the below error
request model:
{
"variant": {
"product_id": 7644173172887,
"option1": "red",
"option2": "small",
"price": "1.0",
"title": "S"
}
}
response :
{
"errors": {
"base": [
"Option values provided for 1 unknown option(s)"
]
}
}
You are not specifing what endpoint you're using but I can see that you're specifing the Product Id and that should not be the case. If you need to add a new variant this is the request you should do
POST https://yourstore.myshopify.com/admin/api/2022-04/products/7644173172887/variants.json
{"variant":{"option1":"Red", "option2" :"Small", "title": "Red", "price":"1.00"}}
You will also need to update the current "Default Title" variant (I suggest, if possible to create the product with a valid variant).
PUT https://yourstore.myshopify.com/admin/api/2022-04/variants/42211487744151.json
{"variant":{"id":42211487744151,"option1":"Red","option2" : "S", "price":"99.00", "title" : "Red"}}

How to get nested "Sequelize" data?

I have created a Sequelize ORM based PostgreSQL database. I have implemented some relation between product and reviews. And, each review has been posted by a user. So, when I am querying for a single product, I want the data with, product, reviews & inside review I want to see who posted as well means user.
{
"id": 1,
"name": "Rounded Neck T-Shirt",
"title": "some title",
"snippet": "some snippet od demin t shirt",
"details": "some details",
"moreInfo": "some moreInfo",
"size": "size will be in a seperate table this will be FK",
"stock": 452,
"askingPrice": 4299,
"offPrice": null,
"offer": null,
"ratings": null,
"images": null,
"pictureUrl": "https://loopinfosol.in/themeforest/ekka-html-v31/ekka-html/assets/images/product-image/6_2.jpg",
"purchaseCost": 4200,
"createdAt": "2022-02-14T10:59:55.999Z",
"updatedAt": "2022-02-14T11:07:12.916Z",
"categoryId": 1,
"userId": 1,
"reviews": [
{
"id": 1,
"rating": 5,
"comment": "Very nice product",
"createdAt": "2022-02-14T17:28:22.680Z",
"updatedAt": "2022-02-14T17:28:22.680Z",
"productId": 1,
"userId": 1
}
]
}
Here, "userId" is the foreign key for the registered user id. I want to see the user data inside "reviews" as well. How can I do that?
I get the above JSON data by querying the following code.
Product.findOne({
include: [Review],
where: { id: req.params.id },
})
If you already have an association definition like Review.belongsTo(User, ... then you simply need to indicate a nested include option with User model:
Product.findOne({
include: [{
model: Review,
include: [{
model: User
}]
}],
where: { id: req.params.id },
})

How to use embedsMany in laravel to get all attributes? Only foreign key is returning

Hello Good Developers,
I am trying to implement embedsMany relationship of jenssegers/laravel-mongodb
I have two collections:
ProfileSection - {
"_id": "5c865ea4257db43fe4007331",
"general_name": "MY_PROFILE",
"type": "public",
"points": 100,
"status": 1,
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"language": "EN",
"text": "My Profile",
"description": "My Profile"
},
...
],
"updated_at": "2019-03-11T13:12:04.000Z",
"created_at": "2019-03-11T13:12:04.000Z"
}
Profile Questions - {
"_id": "5c865ea3257db43fe40072b2",
"id": "STANDARD_EDUCATION",
"general_name": "STANDARD_EDUCATION",
"country_code": "US",
"order": 1,
"profile_section_id": "5c865ea4257db43fe4007331",
"profile_section": "My Profile",
"translated": [
{
"con_lang": "US-EN",
"text": "What is the highest level of education you have completed?",
"hint": null,
"mapping": {},
"answers": [
{
"precode": "1",
"text": "3rd Grade or less",
"mapping": {}
}
]
},
{...}
],
"updated_at": "2019-03-11T13:12:03.000Z",
"created_at": "2019-03-11T13:12:03.000Z"
}
In ProfileSection I have added
public function questions()
{
return $this->embedsMany(ProfilerQuestions::class, '_id', 'profile_section_id');
}
If I execute ProfileSection::find('5c865ea4257db43fe4007331')->questions
It returns me Profile Questions Object with only one attribute: 5c865ea4257db43fe4007331 i.e ObjectId of Profile Section
I tried using ->with('questions) before accessing questions object
like this
but it's not working
I don't understand what's the issue will definitely need some help.

Why is image_hash empty for some ad_creatives? Is it because it is expired?

I ran _">https://graph.facebook.com/act_47121778/adcreatives?access_token=_
and got the following results. Why are some creatives returning empty image hash?
{
"view_tag": "",
"alt_view_tags": [
],
"creative_id": "6002716206572",
"type": 1,
"title": "Grow your fans!",
"body": "Fan Page Owners! Grow your fanbase. Create quizzes for your page. Win $100 Weekly. Make money with our revenue share program.",
"image_hash": "6ac30e43f21c8580361de92d3288ed68",
"link_url": "http://www.facebook.com/quizwriter?sk=app_81434956828",
"name": "Grow your fans!",
"run_status": 1,
"preview_url": "http://www.facebook.com/ads/api/creative_preview.php?cid=6002716206572",
"count_current_adgroups": 1,
"id": "6002716206572",
"image_url": "http://creative.ak.fbcdn.net/v41818/flyers/109/44/1296329249810543669_1_31eb15ed.jpg"
},
{
"view_tag": "",
"alt_view_tags": [
],
"creative_id": "6002444043572",
"type": 1,
"title": "Tennis Champs Social Game",
"body": "Tennis Champ! Start with 5,000 dollars , beat out other players with your mad tennis skills and become the tennis champ! Socialize!",
"image_hash": "",
"link_url": "334886511760",
"name": "Tennis Champs Social Game",
"run_status": 1,
"preview_url": "http://www.facebook.com/ads/api/creative_preview.php?cid=6002444043572",
"count_current_adgroups": 2,
"id": "6002444043572"
},

MongoDB Database Structure and Best Practices Help

I'm in the process of developing Route Tracking/Optimization software for my refuse collection company and would like some feedback on my current data structure/situation.
Here is a simplified version of my MongoDB structure:
Database: data
Collections:
“customers” - data collection containing all customer data.
[
{
"cust_id": "1001",
"name": "Customer 1",
"address": "123 Fake St",
"city": "Boston"
},
{
"cust_id": "1002",
"name": "Customer 2",
"address": "123 Real St",
"city": "Boston"
},
{
"cust_id": "1003",
"name": "Customer 3",
"address": "12 Elm St",
"city": "Boston"
},
{
"cust_id": "1004",
"name": "Customer 4",
"address": "16 Union St",
"city": "Boston"
},
{
"cust_id": "1005",
"name": "Customer 5",
"address": "13 Massachusetts Ave",
"city": "Boston"
}, { ... }, { ... }, ...
]
“trucks” - data collection containing all truck data.
[
{
"truckid": "21",
"type": "Refuse",
"year": "2011",
"make": "Mack",
"model": "TerraPro Cabover",
"body": "Mcneilus Rear Loader XC",
"capacity": "25 cubic yards"
},
{
"truckid": "22",
"type": "Refuse",
"year": "2009",
"make": "Mack",
"model": "TerraPro Cabover",
"body": "Mcneilus Rear Loader XC",
"capacity": "25 cubic yards"
},
{
"truckid": "12",
"type": "Dump",
"year": "2006",
"make": "Chevrolet",
"model": "C3500 HD",
"body": "Rugby Hydraulic Dump",
"capacity": "15 cubic yards"
}
]
“drivers” - data collection containing all driver data.
[
{
"driverid": "1234",
"name": "John Doe"
},
{
"driverid": "4321",
"name": "Jack Smith"
},
{
"driverid": "3421",
"name": "Don Johnson"
}
]
“route-lists” - data collection containing all predetermined route lists.
[
{
"route_name": "monday_1",
"day": "monday",
"truck": "21",
"stops": [
{
"cust_id": "1001"
},
{
"cust_id": "1010"
},
{
"cust_id": "1002"
}
]
},
{
"route_name": "friday_1",
"day": "friday",
"truck": "12",
"stops": [
{
"cust_id": "1003"
},
{
"cust_id": "1004"
},
{
"cust_id": "1012"
}
]
}
]
"routes" - data collections containing data for all active and completed routes.
[
{
"routeid": "1",
"route_name": "monday1",
"start_time": "04:31 AM",
"status": "active",
"stops": [
{
"customerid": "1001",
"status": "complete",
"start_time": "04:45 AM",
"finish_time": "04:48 AM",
"elapsed_time": "3"
},
{
"customerid": "1010",
"status": "complete",
"start_time": "04:50 AM",
"finish_time": "04:52 AM",
"elapsed_time": "2"
},
{
"customerid": "1002",
"status": "incomplete",
"start_time": "",
"finish_time": "",
"elapsed_time": ""
},
{
"customerid": "1005",
"status": "incomplete",
"start_time": "",
"finish_time": "",
"elapsed_time": ""
}
]
}
]
Here is the process thus far:
Each day drivers begin by Starting a New Route. Before starting a new route drivers must first input data:
driverid
date
truck
Once all data is entered correctly the Start a New Route will begin:
Create new object in collection “routes”
Query collection “route-lists” for “day” + “truck” match and return "stops"
Insert “route-lists” data into “routes” collection
As driver proceeds with his daily stops/tasks the “routes” collection will update accordingly.
On completion of all tasks the driver will then have the ability to Complete the Route Process by simply changing “status” field to “active” from “complete” in the "routes" collection.
That about sums it up. Any feedback, opinions, comments, links, optimization tactics are greatly appreciated.
Thanks in advance for your time.
You database schema looks like for me as 'classic' relational database schema. Mongodb good fit for data denormaliztion. I guess when you display routes you loading all related customers, driver, truck.
If you want make your system really fast you may embedd everything in route collection.
So i suggest following modifications of your schema:
customers - as-is
trucks - as-is
drivers - as-is
route-list:
Embedd data about customers inside stops instead of reference. Also embedd truck. In this case schema will be:
{
"route_name": "monday_1",
"day": "monday",
"truck": {
_id = 1,
// here will be all truck data
},
"stops": [{
"customer": {
_id = 1,
//here will be all customer data
}
}, {
"customer": {
_id = 2,
//here will be all customer data
}
}]
}
routes:
When driver starting new route copy route from route-list and in addition embedd driver information:
{
//copy all route-list data (just make new id for the current route and leave reference to routes-list. In this case you will able to sync route with route-list.)
"_id": "1",
route_list_id: 1,
"start_time": "04:31 AM",
"status": "active",
driver: {
//embedd all driver data here
},
"stops": [{
"customer": {
//all customer data
},
"status": "complete",
"start_time": "04:45 AM",
"finish_time": "04:48 AM",
"elapsed_time": "3"
}]
}
I guess you asking yourself what do if driver, customer or other denormalized data changed in main collection. Yeah, you need update all denormalized data within other collections. You will probably need update billions of documents (depends on your system size) and it's okay. You can do it async if it will take much time.
What benfits in above data structure?
Each document contains all data that you may need to display in your application. So, for instance, you no need load related customers, driver, truck when you need display routes.
You can make any difficult queries to your database. For example in your schema you can build query that will return all routes thats contains stops in stop of customer with name = "Bill" (you need load customer by name first, get id, and look by customer id in your current schema).
Probably you asking yourself that your data can be unsynchronized in some cases, but to solve this you just need build a few unit test to ensure that you update your denormolized data correctly.
Hope above will help you to see the world from not relational side, from document database point of view.