How to create vertex and relationship using jSon in Orientdb - orientdb

I have my Json as :-
{ #class : 'Account',
name : 'Luca',
vehicles : [ {
#class : 'Vehicle',
type : 'Car',
model : 'Maserati',
isItTrue: false
} ]
}
I want to create an edge( not link ) between account and vehicle. How can I acheive this?
If not possible by this json format, Can u please provide a json format, using which i can create account and vehicle vertices and relationship between them.
Thanks.

You can use Waterline and sails-oreintdb in your project. I am sure you are looking for this approach.
Cheers

Related

Regarding querying in cloud firestore

I'm doing some research on cloud firestore using flutter. Currently I'm looking into querying in cloud firestore. I got a basic idea of how to query like say in the screenshot of the database given below :
The 2nd (Some Dumb Shit) and 3rd (Some Good Shit) projects belong to the field "Hardware" ..... So if I want to search a project with respect to its field ..... I'll do something like this :
databaseReference.collection("Projects").where("Field",isEqualTo: "Hardware")
But say if I want to search projects based on the name of the members ( Referring to the screenshot above ..... I need to search of a project where a name "Sarvesh Dalvi" is present inside the "Members" field). How am I supposed to write a query in this case.
Note :
Name ("Sarvesh Dalvi") is present inside this heirarchy :
DocumentID(eg : "Some Dumb Shit") / Array("Members") / Map({Name : __ , MemberRef :__});
Thanks in advance for the help.
Update :
I learned how to access an array by doing something like this :
Future<dynamic> getUserProjectFromDatabase(String username)
{
return databaseReference.collection("Projects").where("Members",arrayContains: {"Name" : username}).getDocuments().then((value){
print(value.documents.length);
value.documents.forEach((element) {print(element.data);});
});
}
But this works if the Map only contains :
{"Name" : username};
But in my case my Map is something like this :
{
"Name" : username,
"MemberRef" : /*Reference to a Document*/
};
[Refer to the screenshot posted above]
I only want to query for the Name inside the map and not the MemberRef...... so how can I query something like :
Future<dynamic> getUserProjectFromDatabase(String username)
{
return databaseReference.collection("Projects").where("Members",arrayContains: {"Name" : username,"MemberRef" : /* can be anything */}).getDocuments().then((value){
print(value.documents.length);
value.documents.forEach((element) {print(element.data);});
});
}
There is no way to query for just the member name in your current data structure.
You have two main options:
If you also know the MemberRef for the member, you can query with array-contains: where('Members', arrayContains: {'Name':'New User','MemberRef':'value of memberref'}). This works because array-contains can check if the array contains the complete value that you specify.
If you don't know the MemberRef subbfield, then you'll need to change your data model to allow the query. I typically recommend creating an additional field with just the member names: MemberNames: ["New User", "Sarvesh Dalvi"]. Then you can use the same array-contains operator, but now on this new field with the simple type: where('MemberNames', arrayContains: 'New User').

Mongodb command for generate a new ObjectId using Extended JSON

I have a collection like the one below
I need to push a new object into the "stores" array using extended json.
How can I instruct the Mongodb to create a new ObjectId for the attribute _id ?
I'm wondering something like this:
{
'_id' : { '$oid' : '$<here_some_command_to_generate_new_oid_automatically>' },
'address' : 'my store address',
'contact' : 'John Doe'
}
The solution is specify like this in the json "id":{"$oid": "<ObjectId()>"},
Unless I misunderstood you ,if you just need to generate using client side you can do it the same way any driver would do for top level documents.
In java to generate extended json version you could use JsonWriterSettings with output mode as JsonMode.Extended. Something like
JsonWriterSettings setting = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
Document id = new Document("_id", new ObjectId());
String extendedIdJson = id.toJson(setting);// {'_id' : { '$oid' : '507f191e810c19729de860ea' }}

MongoDB Find and Modify With GraphQL

I am working on GraphQL mutation and need help here. My document looks like
{
"_id" : ObjectId("5bc02db357146d0c385d4988"),
"item_type" : "CategoryMapping",
"id" : null,
"CategoryGroupName" : "Mystries & Thriller",
"CustomCategory" : [
{
"name" : "Private Investigator",
"MappedBisacs" : [
"investigator",
"Privately owned",
"Secret"
]
},
{
"name" : "Crime Investigator",
"MappedBisacs" : [
"crime investigator",
"crime thriller"
]
}
]
}
UI
Allow user to update MappedBisacs through list of checkbox. So user can add/update or delete list of bisacs.
Problem - When client send GraphQL query like following;
mutation {
CategoryMapping_add(input: {CategoryGroupName: "Mystries & Thriller", CustomCategory: [{name: "Crime Investigator", MappedBisacs: ["investigator", "dafdfdaf", "dafsdf"]}]}) {
clientMutationId
}
}
I need to find Specific custom category and update its bisac array.
I am not sure if I got it, but this more a doubt on MongoDb than on GraphQL itself. First you must find the document that you want (I would use the id of the document instead of CategoryGroupName), then you can update this array in several ways. For example, after you found the document, you could simply access the array content and spread into a new one adding this new data from your mutation, and save this object with the update method. (if you simply want to add new data without removing any)
So, it depends on the case.
Check: https://docs.mongodb.com/manual/reference/operator/update-array/
Hope it helps! :)

What's the better way to represent this multidimensional data?

We are trying to represent this data in a web application.What will be the appropriate way to represent this data? We thought of using relational structure but data are hierarchical in nature.Is it better to use MongoDB in this scenario ?
As per comment mongo dynamic schemas is a perfect solution.
let assume that we have our document structured like this:
report {
_id...
documentBycode {
_id : "G8",
dataSource,
remarks,
fields : [{
indicator : "sucide",
baseline {
data,
year,
source
},
milestone[{
year : 2017,
value : 15
}, {}
]
...
...
fields : [{
name : "nfhs1996",
value : "n/a",
order : 1 /* this is not mandatory*/
}, {
name : "ndhs2011",
value : "n/a",
order : 2
}
]
]
}
}
then you can add/modify elements as needed inside [arrays] and always get report data by retrieving only one document from datastore.
What's also could be interesting you could have mulitple diffrent reports structures stored in same collection - so you can literally store full ViewModel data AS IS
Any comment welcome!

Using foreign Keys and mapping to EXTJS

I am using a mongo based DB schema where I have list of ReferenceFields (ObjectIds).
So assume I have two models : User, Group
User Object :
{
"_id" : ObjectId("554ba2897fdd66239457fb33"),
"name" : "something"
}
Group Object :
{
"_id" : ObjectId("453490sdkjskldfjlskdjf"),
"name" : "group_name",
users : [ ObjectId("554ba2897fdd66239457fb33"), ObjectId("554ba28a7fdd66239457fb34") ]
}
ExtJS User Model
Ext.define('Group',{
extend: 'Ext.data.Model',
idProperty : 'id',
fields:[
{name:'group_id', type : 'string', mapping : '_id', persist : false},
{name:'name', type : 'string', mapping : 'name', persist : false}
],
hasMany : { model : 'User', foreignKey:'id' , name : 'users'}
});
The User Model:
Ext.define('User',{
extend: 'Ext.data.Model',
idProperty : 'id',
fields:[
{name:'id', type : 'string', mapping : '_id', persist : false},
{name: 'name', type: 'string'},
]
});
in EXTJS code when I access group.users();
Using the rest method in stores when I add associations to models by giving a foreign Key it fires a GET request to
/users?filter=[{"property":"group_id","value":"453490sdkjskldfjlskdjf","exactMatch":true}]
But this is MySQL kind of association.
How do I solve this to adapt to mongo style of associations?
SO I basically want this to happen. Right now when I call
groups.users().load() it fires a GET request to the Users proxy with the filter params like this
/users?filter=[{"property":"group_id","value":"453490sdkjskldfjlskdjf","exactMatch":true}]. But I don't have group_id in users! So it has to query the users proxy based on the ObjectId it finds inside the users attribute of the group object.
As this is very common in Document based DB, Is this possible by default ? Am I missing something ?
Thanks.
Relationship 1:many in Ext is not implemented the same way as in DBMS. In DBMS we have two tables (a construct similar to store in Ext) and the relationship is defined by foreign keys that link records between tables.
In Ext, we (can) have a store on 1-side (Groups) but we do not have one single store for many-side (Users). Instead, each record of Groups contains a store for Users. It is not exactly a record field for the store but Ext defines a getter.
In your case group.users() returns a store with users that belong to that specific group. Ext behaves exactly same as with any other store when you try to load it: group.users().load() - it only adds filter for that specific foreign key so that you don't need to do it manually.