I have two collection in MongoDB database, i want join two Collection in PHP
I have searched but unfortunately I have not found a compelling answer.
Data look like this:
users
{
"_id": "4ca30369fd0e910ecc000006",
"login": "user11",
"pass": "example_pass",
"date": "2017-12-15"
}
news
"_id": "4ca305c2fd0e910ecc000003",
"name": "news 333",
"content": "news content",
"user_id": "4ca30373fd0e910ecc000007",
"date": "2017-12-15"
}
Already answer in this thread
Note : I'm a MEAN developer
In mean we use .populate() method (mongoose) to achieve joins upto a level.
as for php
You can use different approach from RDBMS
Data Replication
"news": {
"_id": "4ca305c2fd0e910ecc000003",
"name": "news one",
"content": "news one",
"user": {
"_id": "4ca30369fd0e910ecc000006",
"login": "user11"
},
"date": "2017-12-15"
}
Related
I've created two collections "Users" and "Posts".
Users document structure is as follows:
{
"_id": {
"$oid": "54dde0e32a2a999c0f00002a"
},
"first_name": "Vamsi",
"last_name": "Krishna",
"email": "vamshi#test.com",
"password": "5f4dcc3b5aa765d61d8327deb882cf99",
"date_of_birth": "1999-01-05",
"gender": "male",
"status": "Active",
"date_created": "2015-02-13 12:32:50"
}
While posts document structure is:
{
"_id": {
"$oid": "54e1a2892a2a99d00500002b"
},
"post_description": "Test post 1",
"posted_by": {
"id": "54dde0e32a2a999c0f00002a",
"first_name": "Vamsi",
"last_name": "Krishna",
"gender": "male"
},
"posted_on": "2015-02-16 08:55:53",
"comments": [],
"likes": {
"count": 0,
"liked_by": []
}
}
My query is that when user updates his information it should reflect everywhere like posted by, commented by and liked by. How can I achieve that?
I'm using PHP.
Thanks!!
Mongodb does not have a notion similar to sql on update cascade, so you have to do this in your application (whenever you update user information, update all other documents that relate to this user in other collections).
As you might have guessed this is super inefficient when there are a lot of such documents, which means that your schema is bad. Just have a userID in your document and this will link to your user's collection.
I want to make case insensitive search on fields with EdgeNGram analyzer. I am using ES in php via elastica.
I have table of users
{
"user": {
"analyzer": "analyzer_edgeNGram",
"properties": {
"admin": {
"type": "boolean"
},
"firstName": {
"type": "string",
"analyzer": "analyzer_edgeNGram"
},
"lastName": {
"type": "string",
"analyzer": "analyzer_edgeNGram"
},
"username": {
"type": "string",
"analyzer": "analyzer_edgeNGram"
}
}
}
}
My analyzers look like this (you can see there is lowercase filter in egdeNGram analyzer)
"index.analysis.filter.asciifolding.type": "asciifolding",
"index.number_of_replicas": "1",
"index.analysis.filter.standard.type": "standard",
"index.analysis.tokenizer.edgeNGram.token_chars.1": "digit",
"index.analysis.tokenizer.edgeNGram.max_gram": "10",
"index.analysis.analyzer.analyzer_edgeNGram.type": "custom",
"index.analysis.tokenizer.edgeNGram.token_chars.0": "letter",
"index.analysis.filter.lowercase.type": "lowercase",
"index.analysis.tokenizer.edgeNGram.side": "front",
"index.analysis.tokenizer.edgeNGram.type": "edgeNGram",
"index.analysis.tokenizer.edgeNGram.min_gram": "1",
"index.analysis.tokenizer.standard.type": "standard",
"index.analysis.analyzer.analyzer_edgeNGram.filters": "standard,lowercase,asciifolding",
"index.analysis.analyzer.analyzer_edgeNGram.tokenizer": "edgeNGram",
"index.number_of_shards": "1",
"index.version.created": "900299"
There is for example user with firstName Miroslav. If I do query like this
{"query": {"match": {"firstName": "miro"}}}
I have 0 hits. But if I changed in query miro to Miro it will find.
I've checked how are the tokens generated and they are case sensitive: M, Mi, Mir, ...
Any advice how to achieve case insensitive searching?
Thank you
The default search_analyzer set is standard and has the following settings:
"analyzer": {
"rebuilt_standard": {
"tokenizer": "standard",
"filter": [
"lowercase"
]
}
}
So by default your queries must be case insensitive, but you can allways try to set the value of search_analyzer to something else. In the docs:
Sometimes, though, it can make sense to use a different analyzer at search time, such as when using the edge_ngram tokenizer for autocomplete.
By default, queries will use the analyzer defined in the field mapping, but this can be overridden with the search_analyzer setting:
I am trying to integrate the bryntum component(schedule) in php. I am not much aware in ext js.
Please see the images here
Here, Name fields are fetching properly, whereas Capacity is not accessing. These values are coming from Zoho CRM.
My code is like Click, whereas r-read.php file is the responsible file for fetching the record from CRM and store it in a json format. It is like
{
"success": true,
"total": 9,
"root": [{
"Id": 1,
"Name": "Sri Test",
"Capicity": "190.0"
}, {
"Id": 2,
"Name": "tester_test01",
"Capicity": "500.0"
}, {
"Id": 3,
"Name": "Tesing room 23",
"Capicity": "5000.0"
}, {
"Id": 4,
"Name": "Test for 6th product",
"Capicity": "5000.0"
}, {
"Id": 5,
"Name": "Banquet hall test-01",
"Capicity": "500.0"
}, {
"Id": 6,
"Name": "test room",
"Capicity": "1000.0"
}, {
"Id": 7,
"Name": "Grande Ballroom",
"Capicity": "4000.0"
}, {
"Id": 8,
"Name": "Cedar Room",
"Capicity": "1400.0"
}, {
"Id": 9,
"Name": "Maple Room",
"Capicity": "1200.0"
}]
}
In the capacity column, it will show like 190.0 , 500.0, 5000.0 etc like Name column.
I'm not familier with the Bryntum schedular component, but most of the time when you have problems like these it's because you didn't define the Capacity field in your model.
I saw you used the following model: Sch.model.Resource. Can it be that is only has the Name field and not Capacity? Your JSON response looks fine to me.
In the sample JSON above, Capacity is spelled Capicity.
See if the same spelling needs can be used everywhere. Maybe then the data will resolve properly.
Recently, our team is going to develop mobile(iphone, android platforms) applications for our existing website, let user can use the application to more easy to read our content via the application.
But our team have different views in JSON schema of the API return, below are the sample response.
Schema type 1:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": {
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
"user": {
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": {
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
},
"user": {
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/2"
},
"post_ts": "2012-12-01 18:15:00T0800"
}
]
}
}
Schema type 2:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": 100,
"user": 1,
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": 101,
"user": 2,
"post_ts": "2012-12-01 18:15:00T0800"
}
],
"category": [
{
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
{
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
}
],
"user": [
{
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
{
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/Hello_baby"
}
]
}
}
Some Developers claim that if using schema type 2,
can reduce data size if the category & user entities comes too much duplicated. it does really reduce at least 20~40% size of response plain text.
once if the data size come less, in parsing it to JSON object, the memory get less
categoey & user can be store in hash-map, easy to reuse
reduce the overhead on retrieving data
I have no idea on it if schema type 2 does really enhanced. Because I read so many API documentation, never seen this type of schema design. For me, it looks like a relational database. So I have few questions, because I have no experience on designing a web services API.
Does it against API design principle (Easy to read, Easy to use) ?
Does it really get faster and get less memory resource on parsing on IOS / Android platform?
Does it can reduce the overhead between client & server?
Thanks you.
When I do such an application for android, I parse JSON just one and put it in database. Later I'm using ContentProvider to access it. In Your case You could use 2nd schema but without user, category part. Use lazy loading instead but it will be good solution just in case categories and users repeat often.
I'm trying to use PHP to create a JSON representation of a paragraph of text, keeping information about links/formatting etc.
Essentially, I want to convert this string:
"Hello <a href='www.google.com'>World!</a>. How are <b>you</b> today?"
Into these 7 JSON objects:
"1": {
"_id": "1",
"_type": "TEXT",
"value": "Hello "
},
"2": {
"_id": "2",
"_type": "TEXT",
"value": "World!",
"_attributes": {
"3": {
"_id": "3",
"_type": "LINK",
"src": "www.google.com"
}
}
},
"4": {
"_id": "4",
"_type": "TEXT",
"value": " How are "
},
"5": {
"_id": "5",
"_type": "TEXT",
"value": "you",
"_attributes": {
"6": {
"_id": "6",
"_type": "FORMATTING",
"bold": true,
}
}
},
"7": {
"_id": "7",
"_type": "TEXT",
"value": " today?"
}
I've hunted the internet/google and found plenty about splitting HTML, but can't seem to describe what I want. I need to separate the plain text from the link/formatting and create a single entity for each.
The "FORMATTING" attribute just adds "bold"/"underline"/"subscript" etc fields as appropriate.
Nested tags will simply create multiple attributes for their text entity.
I don't yet know how I'd handle a 2-word hyperlink that has one word bolded... perhaps it'll have to have 2 hyperlink attributes.
Any help MUCH appreciated!!
A DOMDocument is what you need. If you can live with slightly different names, you barely have to do any work, too.