:couchbeam.save_doc/2 in Elixir - couchdb

Written some code to save a document to CouchDB in Elixir using Couchbeam:
case :couchbeam.open_or_create_db(server, "database", []) do
{ :ok, db } ->
doc = [
_id: "auth_#{username}",
content: 'some text'
]
case :couchbeam.save_doc(db, doc) do
{ :ok, saved_doc } -> saved_doc
{ :error, :econnrefused } -> %{
:error_code => "save_error",
:error => "Failure to create account"
}
end
{ :error, :econnrefused } ->
IO.puts "Could not connect to server"
end
This results in an error stating that save_doc/4 is not a matching function clause, when it is clearly defined in my local couchbeam.erl.
save_doc/4 is defined as:
-spec save_doc(Db::db(), doc(), mp_attachments(), Options::list()) ->
{ok, doc()} | {error, term()}.
save_doc(#db{server=Server, options=Opts}=Db, {Props}=Doc, Atts, Options) ->
The stacktrace stating no function clause found:
** (FunctionClauseError) no function clause matching in :couchbeam.save_doc/4
stacktrace:
(couchbeam) src/couchbeam.erl:560: :couchbeam.save_doc({:db, {:server, "http://localhost:5984", [basic_auth: {"admin", "admin"}]}, "database", [basic_auth: {"admin", "admin"}]}, [_id: "auth_hello", content: 'some text'], [], [])
(cs) lib/models/Auth.ex:60: anonymous fn/3 in Server.Model.Auth.signup/3
test/model/auth_test.exs:7
Does save_doc/2 require something that the arguments passed in are missing?

It should be
doc = {[
{ "_id", "auth_#{username}" },
{ "content", "some text" }
]}
The type doc() is defined as {[{ejson_key(), ejson_term()}]} in Erlang. The keyword list literal syntax you were using needed to be wrapped in a tuple.

Related

Deleting items from the array of objects

I have a document that looks like:
{
_id: "....",
hostname: "mysite.com",
text: [
{
source: "this is text. this is some pattern",
...
},
{
source: "....",
...
}
]
}
and I am trying to delete the items from the text array which match a specific condition in my query given as:
db.getCollection('TM').updateMany(
{hostname: "mysite.com"},
{
$pull: {
"text.source": /this is some pattern/
}
},
{ multi: true }
)
Here I want to delete all the items from the array where the value inside source matches this is some pattern. When I execute this query, it gives an error saying: Cannot use the part (source) of (text.source) to traverse the element with error code 28.
What is the way to achieve this?
it gives an error saying: Cannot use the part (source) of (text.source) to traverse the element with error code 28.
Incorrect syntax of $pull for update methods,
Corrected syntax, and You can use $regex to find document by specific pattern,
"text.source" the condition in filter part will filter main documents, it is optional
text: { source: will filter sub documents and pull elements
db.getCollection('TM').updateMany(
{
hostname: "mysite.com",
"text.source": { $regex: "this is some pattern" }
},
{
$pull: {
text: { source: { $regex: "this is some pattern" } }
}
}
)
Playground

Combining Cypher and GraphQL unable to traverse relationship node

I am new to Neo4j and have been learning for the past few days about using GraphQL with Neo4j through Grandstack. I've been working through this Guide and this Repository working on setting up schemes. I've been working off of the sample neo4j movie database Im attempting to do a basic query where I select the movie by rating using a cypher query as shown below. How ever when testing in the browser I am receiving the following error. Any idea how to fix this or what I am doing wrong thank you
const typeDefs = `
type Movie {
title: String
tagLine: String
released: Int
reviews: [Reviewed]
}
type Reviewed #relation(name: "REVIEWED"){
from: Person
to: Movie
summary: String
rating: String
}
type Person {
name: String
born: Int
actedIn: [Movie] #relation(name: "ACTED_IN",direction:"OUT")
}
type Query {
Movie(title: String ,released: Int, first: Int, offset: Int): [Movie]
ReviewsByScore(score: Int): [Reviewed] #cypher(statement: "MATCH()-[r:REVIEWED]-() WHERE r.rating >= $score RETURN r;")
}
`;
const schema = neo4jgraphql.makeAugmentedSchema({ typeDefs });
In the browser I run the following query
{
ReviewsByScore(score: 100) {
rating
summary
to{
title
}
}
}
and receive the following error.
{ "errors": [
{
"message": "Cannot read property 'value' of undefined",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"ReviewsByScore"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: Cannot read property 'value' of undefined",
" at getRelationTypeDirective (/Users/a123456/Desktop/Neo4j Test
Javascript/node_modules/neo4j-graphql-js/dist/utils.js:763:7)",
" at buildCypherSelection (/Users/a123456/Desktop/Neo4j Test
Javascript/node_modules/neo4j-graphql-js/dist/selections.js:184:64)",
" at recurse (/Users/a123456/Desktop/Neo4j Test Javascript/node_modules/neo4j-graphql-js/dist/selections.js:87:33)",
" at buildCypherSelection (/Users/a123456/Desktop/Neo4j Test
Javascript/node_modules/neo4j-graphql-js/dist/selections.js:176:12)",
" at recurse (/Users/a123456/Desktop/Neo4j Test Javascript/node_modules/neo4j-graphql-js/dist/selections.js:87:33)",
" at buildCypherSelection (/Users/a123456/Desktop/Neo4j Test
Javascript/node_modules/neo4j-graphql-js/dist/selections.js:176:12)",
" at customQuery (/Users/a123456/Desktop/Neo4j Test Javascript/node_modules/neo4j-graphql-js/dist/translate.js:575:68)",
" at translateQuery (/Users/a123456/Desktop/Neo4j Test Javascript/node_modules/neo4j-graphql-js/dist/translate.js:518:12)",
" at cypherQuery (/Users/a123456/Desktop/Neo4j Test Javascript/node_modules/neo4j-graphql-js/dist/index.js:146:40)",
" at _callee$ (/Users/a123456/Desktop/Neo4j Test Javascript/node_modules/neo4j-graphql-js/dist/index.js:73:31)"
]
}
}
} ], "data": {
"ReviewsByScore": null } }

Getting null while trying to traverse to depth 2 of graphql type using neo4j pattern comprehension in nodejs environment

I am using neo4j(3.1), GraphQL and Nodejs. For now, I have 3 graphql types namely Country, State and City and these types have following relation in Neo4j dB.
(City)-[:PRESENT_IN]->(State)-[:PRESENT_IN]->(Country)
My graphql Type is like below:
Type Country{
name: String,
states: [State]
}
Type State{
name: String,
cities: [City]
}
Type City{
name: String,
id: Int
}
And my Query schema is:
type Query{
countries(limit: Int): [Country]
states(limit: Int): [State]
}
So , when I ran the query "countries" in GraphiQl, I was hoping to get the cities too available in a particular state present in one country i.e. traversing to depth 2 , but I am getting null when it comes to array of "cities" field,
Although the data comes for field "states".
Here is the query execution done in GraphiQL:
countries(limit:1) {
name
states{
name
cities{
name
id
}
}
}
and Here is the execution result:
"countries": {
"name": "Country 1",
"states": [
{
"name": "State A",
"cities": null ---> Here it is coming null
},
]
}
What I wanted to return was:
"countries": {
"name": "Country 1",
"states": [
{
"name": "State A",
"cities": [
{
name: City 1
id: 1
},
]
},
]
}
For my cypher query, I have used pattern comprehension as in this link: https://neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/
My cypher query is:
// For Query "countries"
MATCH (c:Country)
RETURN c{
.name,
states: [(s:State)-[:PRESENT_IN]->(c) | s{.*}]
} LIMIT $limit
//For Query "states"
MATCH (s:State)
RETURN s{
.name,
cities: [(ct:City)-[:PRESENT_IN]->(s) | ct{.*}]
} LIMIT $limit
So, could anyone tell me what I am doing wrong here ? I looked into the neo4j docs and other blogs, but I am unable to figure it out. Any help or insight would be really helpful !
AFAIK, you have to query for those cities in the countries query. Something like this:
MATCH (c:Country)
RETURN c {
.name,
states: [(s:State)-[:PRESENT_IN]->(c) | s {
.*,
cities: [(ct:City)-[:PRESENT_IN]->(s) | ct {.*}]
}]
} LIMIT $limit

Mongo text search with AND operation for multiple words partially enered

{
TypeList" : [
{
"TypeName" : "Carrier"
},
{
"TypeName" : "Not a Channel Member"
},
{
"TypeName" : "Service Provider"
}
]
}
Question :
db.supplies.find("text", {search:"\"chann\" \"mem\""})
For above query I want display :
{
TypeName" : "Not a Channel Member"
}
But I am unable to get my result.
What are changes I have to do in query .
Please help me.
The below query will return your desired result.
db.supplies.aggregate([
{$unwind:"$TypeList"},
{$match:{"TypeList.TypeName":{$regex:/.*chann.*mem.*/,$options:"i"}}},
{$project:{_id:0, TypeName:"$TypeList.TypeName"}}
])
If you can accept to get an output like this:
{
"TypeList" : [
{
"TypeName" : "Not a Channel Member"
}
]
}
then you can get around using the aggregation framework which generally helps performance by running the following query:
db.supplies.find(
{
"TypeList.TypeName": /chann.*mem/i
},
{ // project the list in the following way
"_id": 0, // do not include the "_id" field in the output
"TypeList": { // only include the items from the TypeList array...
$elemMatch: { //... where
"TypeName": /chann.*mem/i // the "TypeName" field matches the regular expression
}
}
})
Also see this link: Retrieve only the queried element in an object array in MongoDB collection

Cannot parse array into defined type

I am using following puppet class
class myclass{
$foo = [{"id" => "bar", "ip" => "1.1.1.1"}, {"id" => "baz", "ip" => "2.2.2.2"}]
map {$foo:}
define map () { notify {$name['id']: } }
}
But this gives me
err: Could not retrieve catalog from remote server: Could not intern from pson: Could not convert from pson: Could not find relationship target "Change_config::Map[ip1.1.1.1idbar]"
warning: Not using cache on failed catalog
err: Could not retrieve catalog; skipping run
What is the reason for this ?
Regards,
Malintha Adikari
Your array contains hashes. The resource declaration syntax works only for arrays of strings.
$foo = ["bar", "baz"]
map {$foo:}
define map () { notify {$name: } }
If you want to pass data with each resource title, you need to
build a hash of your data, not an array of hashes
use the create_resources function
Untested example code:
$foo = {
"bar" => { "ip" => "1.1.1.1" },
"baz" => { "ip" => "2.2.2.2" },
}
create_resources('map', $foo)
define map ($ip="") { notify { "$name has ip $ip": } }

Resources