In Orient DB, I understand a query like this:
match
{class: someNode, as: someNode, where: (id='123')}
.out() {class: someNode as: relatedNode, while:(true), where:(relevance = true)}
return
someNode
Will return someNode with ID 123 if any outnodes has relevance = true. However, what if i want ALL outgoing nodes to be relevance = true connected to someNode? Can I still do a match starting at someNode 123 going out where all = true?
You can try the following:
match
{class: someNode, as: someNode,
where: (id='123' AND out()[relevance != true).size() = 0}
return someNode
[edit]
If you need also connected nodes with the same features, you can do the following:
match
{class: someNode, as: someNode,
where: (id='123' AND out()[relevance != true).size() = 0}
.out(){class: someNode, as: anotherNode,
while: (relevance = true AND out()[relevance != true).size() = 0)
where: (relevance = true AND out()[relevance != true).size() = 0}
return someNode
You can change the WHILE and WHERE conditions to slightly change the behavior regarding which nodes you want to include/exclude based on relevance flag
Related
I have an array like the following structure
[{"id":1,"address":"UK"},{"id":2,"address":"US"}]
I want to fetch all entries from mongodb where id = 1 and address = "UK" OR id = 2 and address = "US"
let's assume your collection is test_collection, this could do the work.
It works well in DataGrip, you may try it in your mongodb client.
const arr = [{"id":1,"address":"UK"},{"id":2,"address":"US"},{"id":2,"address":"CN"}];
arr.forEach((v) => {
db.test_collection.find({id: v.id, address: v.address}).forEach(
function (order) {
let row = {}
row.id = order.id;
row.address = order.address;
// row.other_field = order.other_field;
print(row);
});
})
;
Problem
My goal is to have a unique field within a subset of my collection. In particular the serial number within a product art should be unique. So there can be two similar serial numbers if the art is different but only one if the art is the same.
To check that I wanted to use the mongoose pre middleware (if there is not a better solution on the schema level).
implant.schema.coffee
ImplantModel = require('./implant.model.js')
...
implantSchema = mongoose.Schema({
art: {
type: String,
required: true
},
serialNr: {
type: String,
required: true
},
...
validateImplant = (data, next) ->
err = ""
if !data.art? || data.art == ""
err += "Keine Implantatbezeichnung vorhanden!\n"
if !data.serialNr? || data.serialNr == ""
err += "Keine Seriennummer vorhanden!\n"
return next err if err != ""
inspect ImplantModel
# ImplantModel.findOne({serialNr: data.serialNr, art: data.art}, (err, implantFound) ->
# return next err if err?
# return next "Eine Seriennummer darf nur einmlaig pro Hersteller angelegt werden!"
# return next null
# )
next
implant.model.coffee
mongoose = require('mongoose')
implantSchema = require('./implant.schema.js').getSchema()
try
module.exports = mongoose.model('Implant', implantSchema)
catch err
module.exports = mongoose.model('Implant')
The problem is that ImplantModel seems to be {} at that time. I also tried it with this.constructor.findOne as mentioned here How to query from within Mongoose pre hook in a Node.js / Express app? which gives me findOne is not a function
I could perform a query before I call .save method but I guess the hook would be the better place.
I have this:
temp = place where (_.name matches p2) fetch()
Now, what i have to do to get just two field of that results? For example name and id.
Thanks in advance :)
With rogue, you can use :
.select()
In your case :
temp = place where (_.name matches p2) select(_.name, _.id) fetch()
More example are available here (go to line 174) :
Rogue QueryTest.scala
please find below code for getting selected filed in mongodb.
db.user.find( { role: 'admin' }, { name: 1, id: 1 } )
You need to use the projection parameter which can limit the results to specific fields:
val q = MongoDBObject.empty
val fields = MongoDBObject("userid" ->, name" -> 1)
for (x <- mongoColl.find(q, fields)) println(x)
Assuming the basic connection and collections are properly referenced, the above code would return only userId and name.
switch #user && #other
when 'user' && true
...
when 'user2' && false
...
Is something like this possible? It's not working for some reason. Thanks!
It's a pity that JS doesn't think [1, 2] === [1, 2] (since they're different references); otherwise you could use arrays to do what you want.
Instead, here's a function:
multiSwitch = (values, cases...) ->
for c in cases
match = true
for i in [0...values.length]
unless c[i] is values[i]
match = false
break
return c[values.length]() if match
return
Use it like this:
multiSwitch [#user, #other],
['user', true, ->
console.log 'case 1'
]
['user2', false, ->
console.log 'case 2'
]
Depending on what you're doing, it may be easier to, say, concatenate your multiple values into a string and do a switch on that.
I have a hash coming back from an XML datasource that looks like this:
{...,
'records' :{
'record' :[
{'availability' :{'$t' :'available'}, ...},
{'availability' :{'$t' :'available'}, ...}
]
}
};
I'd like to get all the record hashes into an array so I can filter() it and do some other operations. However, when I have this statement in my pre block,
raw_records = raw.pick("$..record");
the array that gets returned is an array of two empty strings:
var raw_records = ['', ''];
The odd thing is that I can pick out just availability with expected results:
availability = raw.pick("$..availability.$t");
producing
var availability = ['available', 'available'];
What's wrong with my first pick()?
EDIT: Here is a more complete version that should help with reproducing the problem. It's slightly different, since I'm using the JSON version of the web service now:
global {
datasource hbll <- "https://svc.lib.byu.edu/services/catalog/v1/search/?field=isbn&format=json&terms=";
}
rule new_rule {
select when pageview "amazon.com/.*/?dp/(.*)/" setting (isbn)
pre {
//This is the array with two empty strings...
raw = datasource:hbll(isbn);
myfilter = function(x) { x.pick("availability") eq "available"; };
records = raw.filter(myfilter);
len = records.length();
availability = records.pick("$..availability");
middleman = len > 1 => availability[0] | availability;
available = middleman eq "available" => true | false;
url_list = records.pick("$..url");
url = len > 1 => url_list[0] | url_list;
msg = <<
<p>This book is available for checkout at the BYU Library.</p>
More information
>>;
}
notify("BYU Harold B. Lee Library", msg) with sticky=true;
}
I'm going to need a more complete example. The test app and results I got are below:
ruleset a8x167 {
meta {
name "Pick - Array of Hashes"
description <<
Testing
>>
author "Sam Curren"
logging on
}
dispatch {}
global {
raw = {
'records' :{
'record' :[
{'availability' :{'$t' :'available'}},
{'availability' :{'$t' :'available'}}
]
}
};
}
rule test {
select when pageview ".*" setting ()
pre {
raw_records = raw.pick("$..record");
availability = raw.pick("$..availability.$t");
}
notify("Hello World", "This is a sample rule.");
}
}
And Results:
var raw_records = [{'availability' :{'$t' :'available'}}, {'availability' :{'$t' :'available'}}];
var availability = ['available', 'available'];