2
class Picture

  property :name
  property :user_ids

  serialize :user_ids
end

Picture.create(name: '1', user_ids: [1,4,5])

How to find all pictures having user id 1 in user_ids property

I am trying this in way:

Picture.as(:p).where('ALL(x IN [1] WHERE x IN p.user_ids)')

Neo4j.rb version - neo4j (8.0.13) Neo4j - 3.1.1 Enterprise

But its not working. Or there is any better way to store array and query it as well, Any help would be very appreciable


EDIT: Shown Bruno's Example & My Node description above I have used example node

enter image description here

@Bruno example is working fine.

Here is my Neo4j node view

enter image description here

match (p:Place) where ALL(x IN [14] WHERE x IN p.from_airport_ids) return p

This is not working in my case

class Place
  include Neo4j::ActiveNode
  property :name
  property :from_airport_ids
  serialize :from_airport_ids
end

Create Place through API

{  
  "place":{  
    "name":"Name",
    "from_airport_ids":[14,44,67]
  }
}

Is it related to how my array values stored in node and how @Bruno example values are stored?

Vishal G
  • 1,521
  • 11
  • 30
  • Bruno's solution is probably the best, but I suspect that the reason your solution isn't working is because you're using `ALL`. I think that `ANY` would work better – Brian Underwood Sep 28 '17 at 21:57
  • @BrianUnderwood I created a node with `create(:Picture{name: '1', user_ids: [1,4,5]})`, performed the query `MATCH (p) where ALL(x IN [1] WHERE x IN p.user_ids) return p` and worked. But I agree with you that the approach using the IN operator is at least more readable. – Bruno Peres Sep 28 '17 at 22:05
  • @BrianUnderwood Maybe has some error message not informed – Bruno Peres Sep 28 '17 at 22:07
  • I edited the Question and gave more info I can see the example @bruno gave is working fine. But in my case there is difference how array values stored in node [1,2,3] vs 1,23 – Vishal G Sep 29 '17 at 14:19
  • @BrianUnderwood I think issue is in Neo4j.rb when I use serialize to store array, it is getting stored as string in neo4j. so I am not able to query it.. If I remove serialize and then save, it is storing as array in neo4j but when I find it like `Place.find(uuid)` it is giving error `TypeError: no implicit conversion of Array into String` in Neo4j lib – Vishal G Sep 29 '17 at 16:59

1 Answers1

2

Try changing your query to:

Picture.as(:p).where('1 IN p.user_ids)')

That is: match all ps where p.user_ids contains 1.

Also, I believe you should store relationships between :User and :Picture nodes instead of an array of ids into :Picture. It's a more graph-way to do things.

Something like

            -[:CONNECTED_TO]->(:User {id:1})
          /
(:Picture)-[:CONNECTED_TO]->(:User {id:2})
          \
            -[:CONNECTED_TO]->(:User {id:3})
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • I have edited the question, above query is also not working, your example (in comment) works good, Also I have given real Node description in question. Picture, user were just examples, But thanks for explaning it – Vishal G Sep 29 '17 at 14:22
  • @V.G `I created a node with` create (:Place { name:"Name", from_airport_ids:[14,44,67]}) and ran the query `match (p:Place) where ALL(x IN [14] WHERE x IN p.from_airport_ids) return p` it's returns me node. It's not the desired behavior? – Bruno Peres Sep 29 '17 at 14:50
  • When I create the node directly form neo4j console then query works good, but when I create it through Active::Node then same query is not working, I uploaded snapshot in chatroom – Vishal G Sep 29 '17 at 15:01