I am using json to create nodes in noe4j
I have written a small c++ prog to do so using curl and json
Now i have to create around 10000 nodes in neo4j with properties having name and value.
For that i am using props in json with the query as
{
"params" : {
"props" : {
[{name : "a", value : 1}, {name : "b", value : 2}......so on]
]
}
},
"query" : "CREATE (n:Router { props }) RETURN n"
}
the question is I just want to create that nodes with unique names. If a node is already present with the name as in json props I do not want to create it.
How to write a query for these types of request in neo4j
Change your query to the following:
{
"params" : {
"props" : {
[{name : "a", value : 1}, {name : "b", value : 2}......so on]
]
}
},
"query" : "FOREACH (router in {props} | MERGE (n:Router {name: router.name}) ON CREATE SET n = router)"
}
Basically it iterate the items in your list, check for name property if it exist and it case it save a new node
Related
I'm looking for a query which can find all of the nodes which have properties that match a parameter which is an object. The properties of the node can be a superset (contain more properties than the filter)
e.g.
:param obj : { first : "first" }
CREATE (n:Test { first : "first", second : "second" });
CREATE (m:Test { first : "first" });
CREATE (f:Fail { first : "bad", second : "second" });
MATCH (c)
WHERE
PROPERTIES(c) = $obj
RETURN c;
n and m should be returned as they are matching on first : "first"
it is doable with apoc, basically by matching the obj with a submap of the properties, containing only the keys that are also present in obj
WITH { first : "first" } AS obj
MATCH (c)
WHERE apoc.map.submap(properties(c),keys(obj),[],false)= obj
RETURN c
Sample json:
{
"data": [
{
"file" : "1.txt",
"type" : "text"
},
{
"file" : "2.json",
"type" : "json"
},
{
"file" : "1.html",
"type" : "html"
}
]
}
I am trying to create 3 nodes with file and type as properties
I am using following query to create nodes in neo4j
WITH {json} AS document
UNWIND document.data AS data
FOREACH (node in data| CREATE(m:`member`) SET m = node )
I am getting following error when i use py2neo driver:
AttributeError: 'module' object has no attribute 'SyntaxError'
In your case either use UNWIND or FOREACH
WITH {json} AS document
UNWIND document.data AS data
CREATE(m:`member`) SET m = data
or
WITH {json} AS document
FOREACH (node in document.data | CREATE(m:`member`) SET m = node )
Query should be like -
WITH {json} AS document
FOREACH (node in document.data| CREATE(m:`memeber`) SET m = node )
I'm using the rest api, and cypher. How do I get back the primary key when doing a query like this for a node with some id that I have assigned to it?
{"statements" : [ {"statement" : "MATCH (n) where n.id = { id } RETURN n",
"parameters" : {
"id" : "1001"
}
}]
}
This will return
{"results":[{"columns":["n"],"data":[{"row":[{"id":"1001"}]}]}],"errors":[]}
Is there a way to get the Neo4J primary key as well?
If, by "primary key", you mean the neo4j-assigned node ID, you can use the ID() Cypher function. For example:
{"statements" : [ {"statement" : "MATCH (n) where n.id = { id } RETURN n, ID(n)",
"parameters" : {
"id" : "1001"
}
}]
}
So I'm trying to create a tree using a really basic parameterised cypher command, but I'm getting this error whenever I try to create more than one item at a time:
If you create multiple elements, you can only create one of each.
{
"query" : "MATCH (p) WHERE p.id='Hello' CREATE (c {props}), p-[r:CHILD]->c",
"params" : {
"props" : [ {
"type": 44,
"title" : "TestNode"
},{
"type": 45,
"title" : "TestNode"
} ]
}
}
What am I doing wrong?
When you pass an array of maps in the CREATE statement, you cannot also create relationships in the same statement.
By providing Cypher an array of maps, it will create a node for each map. When you do this, you can’t create anything else in the same CREATE statement.
See it in the docs here.
All you need to do is add another CREATE statement:
{
"query" : "MATCH (p) WHERE p.id='Hello' CREATE (c {props}) CREATE UNIQUE p-[:CHILD]->c",
"params" : {
"props" : [ {
"type": 44,
"title" : "TestNode"
},{
"type": 45,
"title" : "TestNode"
} ]
}
}
I have a document that has an array:
{
_id: ObjectId("515e10784903724d72000003"),
association_chain: [
{
name: "Product",
id: ObjectId("4e1e2cdd9a86652647000003")
}
],
//...
}
I'm trying to search the collection for documents where the name of the first item in the association_chain array matches a given value.
How can I do this using Mongoid? Or if you only know how this can be done using MongoDB, if you post an example, then I could probably figure out how to do it with Mongoid.
Use the positional operator. You can query the first element of an array with .0 (and the second with .1, etc).
> db.items.insert({association_chain: [{name: 'foo'}, {name: 'bar'}]})
> db.items.find({"association_chain.0.name": "foo"})
{ "_id" : ObjectId("516348865862b60b7b85d962"), "association_chain" : [ { "name" : "foo" }, { "name" : "bar" } ] }
You can see that the positional operator is in effect since searching for foo in the second element doesn't return a hit...
> db.items.find({"association_chain.1.name": "foo"})
>
...but searching for bar does.
> db.items.find({"association_chain.1.name": "bar"})
{ "_id" : ObjectId("516348865862b60b7b85d962"), "association_chain" : [ { "name" : "foo" }, { "name" : "bar" } ] }
You can even index this specific field without indexing all the names of all the association chain documents:
> db.items.ensureIndex({"association_chain.0.name": 1})
> db.items.find({"association_chain.0.name": "foo"}).explain()
{
"cursor" : "BtreeCursor association_chain.0.name_1",
"nscanned" : 1,
...
}
> db.items.find({"association_chain.1.name": "foo"}).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 3,
...
}
Two ways to do this:
1) if you already know that you're only interested in the first product name appearing in "association_chain", then this is better:
db.items.find("association_chain.0.name":"something")
Please note that this does not return all items, which mention the desired product, but only those which mention it in the first position of the 'association_chain' array.
If you want to do this, then you'll need an index:
db.items.ensureIndex({"association_chain.0.name":1},{background:1})
2) if you are looking for a specific product, but you are not sure in which position of the association_chain it appears, then do this:
With the MongoDB shell you can access any hash key inside a nested structure with the '.' dot operator! Please note that this is independent of how deeply that key is nested in the record (isn't that cool?)
You can do a find on an embedded array of hashes like this:
db.items.find("association_chain.name":"something")
This returns all records in the collection which contain the desired product mentioned anywhere in the association_array.
If you want to do this, you should make sure that you have an index:
db.items.ensureIndex({"association_chain.name":1},{background: 1})
See "Dot Notation" on this page: http://docs.mongodb.org/manual/core/document/
You can do this with the aggregation framework. In the mongo shell run a query that unwinds the documents so you have a document per array element (with duplicated data in the other fields), then group by id and any other field you want to include, plus the array with the operator $first. Then just include the $match operator to filter by name or mongoid.
Here's the query to match by the first product name:
db.foo.aggregate([
{ $unwind:"$association_chain"
},
{
$group : {
"_id" : {
"_id" : "$_id",
"other" : "$other"
},
"association_chain" : {
$first : "$association_chain"
}
}
},
{ $match:{ "association_chain.name":"Product"}
}
])
Here's how to query for the first product by mongoid:
db.foo.aggregate([
{ $unwind:"$association_chain"
},
{
$group : {
"_id" : {
"_id" : "$_id",
"other" : "$other"
},
"association_chain" : {
$first : "$association_chain"
}
}
},
{ $match:{ "association_chain.id":ObjectId("4e1e2cdd9a86652647000007")}
}
])