在Neo4j中將對象存儲為屬性

[英]Storing object as property in Neo4j


I need to store an array of User objects inside a Tile node. Each User object contains three primitive properties; Id(a single alpha-character string) , fName and lName. This list of objects is a property of the Tile node with several other primitive properties. The entire Tile node needs to be serialized to Json, including the nested User objects.

我需要在Tile節點中存儲一組User對象。每個User對象包含三個原始屬性; Id(單個字符串字符串),fName和lName。此對象列表是Tile節點的一個屬性,具有多個其他基本屬性。整個Tile節點需要序列化為Json,包括嵌套的User對象。

I understand that Neo can't store complex objects as properties. I created the User as a separate node with id, fName and lName as properties, and I can get these returned via Cypher. I can also get Json output results for the parent Tile node. (In this case, Users is just a string of comma-separated alphas). But how do I get the User node output nested inside the parent node?

我知道Neo不能將復雜對象存儲為屬性。我創建了User作為一個單獨的節點,id,fName和lName作為屬性,我可以通過Cypher返回這些節點。我還可以獲取父Tile節點的Json輸出結果。 (在這種情況下,Users只是一串以逗號分隔的alphas)。但是如何將用戶節點輸出嵌套在父節點中呢?

I have created a list of User objects (userList) by relating user objects with the string of user ids in the Tile Node via a Cypher Query. I just need to get from two separate json outputs to a single nested output.

我通過Cypher查詢將用戶對象與Tile Node中的用戶ID字符串相關聯,創建了User對象列表(userList)。我只需要從兩個單獨的json輸出到一個嵌套輸出。

I hope this is enough detail. I'm using Neo4j 2.1.6 and Neo4jClient. I'm also using .Net 4.0.

我希望這是足夠的細節。我正在使用Neo4j 2.1.6和Neo4jClient。我也在使用.Net 4.0。

3 个解决方案

#1


You could do something like this with cypher and have the cypher return a composite object.

你可以用cypher做這樣的事情並讓密碼返回一個復合對象。

MATCH (t:Tile)-[:CONTAINS_USER]-(u:User)
WHERE t.name =~ 'Tile.*'
WITH {name: t.name, users: collect(u) } AS tile
RETURN collect(tile) AS tiles 

#2


You shouldn't store another object as a nested property. As you correctly state, neo4j doesn't support that but even if it did, you shouldn't do it, because you should link the two with a relationship. That's the key strength of a graph database like neo4j, so you should play to that strength and use the relationships.

您不應將另一個對象存儲為嵌套屬性。正如你所說的那樣,neo4j並不支持這一點,但即使它確實如此,你也不應該這樣做,因為你應該將這兩者聯系起來。這是像neo4j這樣的圖形數據庫的關鍵優勢,所以你應該發揮這種優勢並使用關系。

The server has a default JSON format that tends to output nodes as their own JSON objects. That means that practically speaking, since you're going to model this as two separate nodes with a relationship, you can't get the server by default to nest the JSON for one object underneath of the other. It won't nest the JSON that way because that's not how the data will be stored.

服務器具有默認的JSON格式,傾向於將節點作為自己的JSON對象輸出。這意味着實際上,由於您要將其建模為具有關系的兩個單獨節點,因此默認情況下無法將服務器嵌套到另一個對象下面的JSON。它不會以這種方式嵌套JSON,因為這不是數據的存儲方式。

In this case, I'd use the REST services to fetch the JSON for each object individually, and then do the nesting yourself in your code -- your code is the only place where you'll know which property it should be nested under, and how that should be done.

在這種情況下,我將使用REST服務單獨為每個對象獲取JSON,然后在代碼中自己進行嵌套 - 您的代碼是唯一可以知道它應該嵌套在哪個屬性的地方,以及如何做到這一點。

#3


In addition to these answers, note that if you don't need to involve the subfield properties in any of your queries (e.g. search for Tiles where a User.name is "X"), you can simply serialise the objects fields to a string before insertion (e.g. with JSON.stringify), and unserialise them when reading from the DB.

除了這些答案之外,請注意,如果您不需要在任何查詢中涉及子字段屬性(例如,搜索User.name為“X”的Tiles),您只需將對象字段序列化為字符串即可在插入之前(例如使用JSON.stringify),並在從DB讀取時取消序列化。

This is especially useful when you want to "attach" structured data to a node, but that you don't much care about this data with regards to the relations in your DB (e.g. User preferences).

當您想要將結構化數據“附加”到節點時,這尤其有用,但是您並不關心數據庫中關系的這些數據(例如用戶首選項)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/12/72f4bed4abf0f5bbc69c3cdc6263c078.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com