Let's say we have the following relations in our database:
A Company, that can have zero to many employees
An Employee is hired by one and only one Company
An Employee can be the manager of zero or many employees
...and the following entities in our database:
Company: Id=1, Name='Contoso'
Employee: Id=1, CompanyId:1, ManagerId= Null, Name='Jack Sparrow', Salary=1000
Employee: Id=2, CompanyId:1, ManagerId= 1, Name='Will Turner', Salary=900
...which will (in my case) result in this JSON representation when converted by NewtonSoft:
{
Id: 1,
Name: 'Contoso',
"Employees": [
{Id:1, CompanyId:1, ManagerId: null, Name: 'Jack Sparrow', Salary:1000},
{Id:2, CompanyId:1, ManagerId: 1, Name: 'Will Turner', Salary:900,
Manager:{Id:1, CompanyId:1, ManagerId: null, Name: 'Jack Sparrow', Salary:1000}
}
]
}
As you can see, there are two instances of the Employee/Manager 'Jack Sparrow'.
When binding one of these instances to the UI, I would like both instances to stay in sync if changes occurs.
Are there any mechanism (or trick) in AngularJS v1 which could help me achieve this?
Please note that this is just a simple data structure to illustrate my problem.
One could argue that the 'Manager'-object should not be fully represented, but in my case it is...
Change your query so that it doesn't return the Manager property. Then just fix up the data when you receive it.
I assume your ManagerId field for Will Turner is supposed to be 1 (it's 2 in the JSON sample) in your example to refer to Jack Sparrow. If so all you need is something like:
angular.forEach(employees, function(emp) {
if(emp.ManagerId !== null) {
emp.Manager = employees[emp.ManagerId]);
}
});
Of course you don't even need to change the query not to return the manager, you can still just run this code when you receive the data and overwrite the redundant manager data.
Related
I'm building a laravel application where i have a Reservation model. After the new record is created, the user may want to update it.
I'm looking for a solution to check if the UpdateRequest values are the same of the actual records and if there are updates only update the modified values.
Example, the original record has :
{'id' => 1, 'name' = 'Jhon', surname = 'Doe'}
While the UpdateRequest has
{'id' => 1, 'name' = 'Jhon', surname = 'Kirby'}
So, my questions are :
How do i check which values are different?
How do i update only the
specified values?
I've tried to look at the documentation of laravel but it doesn't seem to refer to this situation, is there any solution or should I just update all the values?
Thank you
I am trying to create two collections in mongoDB. The collections are named roles and users. I read that _id is self-generated id returned by MongoDB on each document insertion. But if we try to insert into the document with the _id key then there is no new Object ID generated.
So I created a collection roles like below-
db.roles.insertOne({"_id" : 101, "name": "admin", "type":1})
db.roles.insertOne({"_id" : 102, "name": "guest", "type":1})
Now, I tried to use these _ids of roles collection in my users table like below -
db.users.insertOne({"username": "test", "password": "test", role: ObjectId(101)})
But it throws me an error saying -
invalid object id: length :
However, if I try to insert a Mongo generated ID, something like ObjectId("600045071dbbafd62cdd6045"), it is able to insert the document.
Can anyone please tell me, what I might be doing wrong?
you need to insert the role same way like the _id, role:101
I am using cloudant NoSQL database to store the historical data. I want the attributes in data column in a separate column. Please help!
Data in the database
It looks like your data has this form:
{
d: {
id: 'xyz',
ts: 123,
ax: 'abc'
}
}
Cloudant's grid view only renders keys at the top of the object tree. If you stored data like this:
{
id: 'xyz',
ts: 123,
ax: 'abc'
}
then the grid view would show id/ts/ax in their own columns.
I'm trying to import documents from a SQL Server database. Each document will have a list of products that a customer has bought, for example:
{
"name": "John Smith"
"products": [
{
"name": "Pencil Sharpener"
"description": "Something, this and that."
},
{
"name": "Pencil case"
"description": "A case for pencils."
}
]
}
In the SQL Server database, the customer and products are stored in separate tables with a one-to-many relationship between the customer and products:
Customer
Id INT
Name VARCHAR
Product
Id INT
CustomerId INT (FK)
Name VARCHAR
Description VARCHAR
I've checked through the documentation , but can't see any mention of how to write the SQL query to map the one-to-many relationships to a single document.
I think there may be a way to do it as on the Target Information step (and when selecting DocumentDB - Bulk import (single partition collections)) there's the option to provide a bulk insert stored procedure. Maybe the products can be assigned to the document's products array from within there. I'm just not sure how to go about doing it as I'm new to Cosmos DB.
I hope that's clear enough and thanks in advance for your help!
It seems that you’d like to return products info formatted as json when you import data from SQL Server using the Azure Cosmos DB: DocumentDB API Data Migration tool. Based on your customer and products table structure and your requirement, I do the following test, which works fine on my side. You can refer to it.
Import data from SQL Server to JSON file
Query
select distinct c.Name, (SELECT p.Name as [name], p.[Description] as [description] from [dbo].[Product] p where c.Id = p.CustomerId for JSON path) as products
from [dbo].[Customer] c
JSON output
[
{
"Name": "Jack",
"products": null
},
{
"Name": "John Smith",
"products": "[{\"name\":\"Pencil Sharpener\",\"description\":\"Something, this and that\"},{\"name\":\"Pencil case\",\"description\":\"A case for pencils.\"}]"
}
]
Parsing the products
On the 'Target Information' step, you'll need to use your own version of BulkTransformationInsert.js. On line 32 is a 'transformDocument' function where you can edit the document. The following will parse the products and then assign them back to document before returning;
function transformDocument(doc) {
if (doc["products"]) {
let products = doc["products"];
let productsArr = JSON.parse(products);
doc["products"] = productsArr;
}
return doc;
}
I am following Link to integrate cloudant no sql-db.
There are methods given create DB, Find all, count, search, update. Now I want to update one key value in one of my DB doc file. how Can i achieve that. Document shows like
updateDoc (name, doc)
Arguments:
name - database name
docID - document to update
but when i pass my database name and doc ID its throwing database already created can not create db. But i wanted to updated doc. So can anyone help me out.
Below is one of the doc of may table 'employee_table' for reference -
{
"_id": "0b6459f8d368db408140ddc09bb30d19",
"_rev": "1-6fe6413eef59d0b9c5ab5344dc642bb1",
"Reporting_Manager": "sdasd",
"Designation": "asdasd",
"Access_Level": 2,
"Employee_ID": 123123,
"Employee_Name": "Suhas",
"Project_Name": "asdasd",
"Password": "asda",
"Location": "asdasd",
"Project_Manager": "asdas"
}
So I want to update some values from above doc file of my table 'employee_table'. So what parameters I have to pass to update.
first of all , there is no concept named table in no sql world.
second, to update document first you need to get document based on any input field of document. you can also use Employee_ID or some other document field. then use database.get_query_result
db_name = 'Employee'
database = client.create_database(db_name,throw_on_exists=False)
EmployeeIDValue = '123123'
#here throw_on_exists=False meaning dont throw error if DB already present
def updateDoc (database, EmployeeIDValue):
results = database.get_query_result(
selector= { 'Employee_ID': {'$eq': EmployeeIDValue} }, )
for result in results:
my_doc_id = result["_id"]
my_doc = database[my_doc_id] #===> this give you your document.
'''Now you can do update'''
my_doc['Employee_Name'] = 'XYZ'
my_doc.save() ====> this command updates current document