Let's say i have a lot of data with this pattern in mongoDB:
db.customer.find()
{'_id':ObjectId('someId'),'customer':'brendon','address':'singapore','activity':[{'id':someId,'detail':'approaching'},{'id':someId,'detail':'explaining'},{'id':someId,'detail':'deal'}]}
this data means i have customer collection and each customer has array of activity,
now, for all customer i want to do paging/sorting the activity about 10 activity per page base on customer/document.
and
how to do this in mongoDB.
in relational sql this is represented with:
customer (custId, name, address)
activity (actId, detail, custId)
select * from activity
left join customer
limit 10,0
Related
looking for some help designing a solution for Search functionality using org.springframework.data.jpa.repository.JpaSpecificationExecutor interface.
Consider a Parent table(P) having columns ID, Name, UUID and some other columns specific to my application.
Now this Parent table has multiple child tables having one-to-many relation, linked via ID column of Parent table, say Child table 1 (C1) and so on.
My sample api request body:
{
"searchText": "",
"column1": ""
}
My requirement today is that the application should be able to perform an OR search between P.ID, P.Name, P.UUID and C1.column2 as part of "searchText" from above payload and then I also should be able to perform AND operation for every field provided in the payload.
Note that the fields in payload and the columns to which it belongs is fixed and cannot be given on runtime and search on fields other than searchText is working fine.
It was working fine till the application was supporting only P.ID, P.Name and P.UUID for searchText, but when I added C1.column2 in query using joins, it started returning duplicate entries as many number of times as the records present in C1 table for P.ID.
Looking for some suggestions for the implementation. Please let me know if my question is not clear or more information is required.
I am new to the core data and swift, I need to get the unique name and count of particular category which has occur in Product rows.
Here is the core data entity structure.
Product: category:String, name:String, store:String
using core data I want to execute the following sql like query
select category, count(*) as total from Product group by category order by category desc.
Please help me to get it done in swift3 with complete reference.
I have two Documents in my Spring data - MongoDB application:
The first one is Contact and looks like this:
public class Contact {
...
private List<Account> accounts;
and the second one is Account and looks like this:
public class Account {
...
private Contact contact;
My question now is, whether there is a better way of:
1. create contact object
2. save contact object into database
3. create account object
4. set contact object into account object
5. save account object into database
6. set created account object into contact object
7. update contact object
These are many steps and I will avoid to do such a long list to get Contact and Account connected bidirectional.
Try this approach
MongoDB is a NOSQL DB and hence there is no need of an order to be preserved, such as create and store contact object and then do so more in a sequential way.
Maintain a sequence for Contact and Account object. Before storing these two records get the next number in the sequence and insert the Contact and Account documents.
References for autoincrement sequence
https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/
https://www.tutorialspoint.com/mongodb/mongodb_autoincrement_sequence.htm
Pseudo Code:
Get the next Sequence of Contact and Account Id
Add the id's to respective documents
Insert the Documents in Mongodb
While retrieving the records you can use $lookup which is a left outer join.
Please note that chance of loss of integrity in data can happen if one insert is happened successfully and other insert did not happen for some reason.
We dont have transaction support in Mongodb across collections, more info.
I don't know whether I should be drawing parallels, but unfortunately, that's the only way I can express my issue.
In SQL, suppose we have two tables:
Employee with columns Employee ID, Employee Name, Dept. ID
Deptartment with columns Dept. ID, Dept Name
The Dept ID. in the Employee table is a foreign key with that in the Department table.
Now suppose I want to fetch the following columns:
Employee ID, Employee Name, Department Name
using a SQL such as:
SELECT A.EMPLOYEE_ID, A.EMPLOYEE_NAME, B.DEPT_NAME
FROM EMPLOYEE A, DEPARTMENT B
WHERE A.DEPT_ID = B.DEPT_ID
How would one do this using Core Data in Swift? I guess I'm getting confused by only seeing references to
NSFetchRequest(entityName: entityName)
where the entityName refers to a single entity (table in relational DB lingo)
I'd really appreciate any pointers or examples that can help me clear my doubts.
Thanks
It is certainly possible to create a fetch request that is equivalent to your SQL query. More complex queries can be difficult if not impossible to achieve with a single fetch request. But I recommend trying NOT to draw parallels between CoreData and SQL, at least until you have got to grips with how it works.
To take your example, in the CoreData view of the world, Employee would be an entity with a relationship to another entity, Department. A fetch request based on the Employee entity will return an array of Employee objects, and (assuming you create subclasses of NSManagedObject for each entity) you can access the attributes with simple dot notation:
let employeeName = myEmployeeObject.employeeName
But you can use the same notation to traverse relationships equally easily:
let departmentName = myEmployeeObject.department.departmentName
You don't need to worry about joins, etc; CoreData handles that for you.
Now, suppose you try to do it "the SQL way". You can construct a fetch request based on the Employee entity, but specify "properties to fetch" which likewise traverse the relationship:
let fetch = NSFetchRequest(entity:"Employee")
fetch.propertiesToFetch = ["employeeID", "employeeName", "department.departmentName"]
For this to work, you would need to specify the "resultType" for the fetch request to be DictionaryResultType:
fetch.resultType = .DictionaryResultType
The results from this query would be an array of dictionaries containing the relevant keys and values. But the link with the underlying objects is lost. If you subsequently want to access any details from the relevant Department (or even the Employee), you would have to run a new fetch to get the object itself.
I am trying to come up with a MongoDB document model and would like others opinions. I want to have a Document that represents an Employee. This table will contain all attributes of an employee (I.e. firstName, LastName). Now where I am stuck coming from the relational realm, is the need to store a list of employees an employee can access. In other words lets say Employee A is a Manager. I need to store the direct reports that he manages, in order to use this in various applications. In relational I would have a mapping table that tied an employee to many employees. In mongo not being able join documents, do you think I should utilize an embeded (sub-document) to store the list of accessible employees as part of the Employee document? Any other ideas ?
Unless your using employee groups (Accounting, HR, etc) You'll probably be fine adding the employee name, mongo Object ID, and any other information unique to that manager / employee relationship as a sub document to the managers document.
With that in place you could probably do your reporting on these relationships through a simple aggregation.
This is all IMHO, and begs the question; Is simple aggregation another oxymoron like military intelligence?