How to retrieve the result of Pregel ArangoDB? - arangodb

I am a newbie and using ArangoDB 3.2.5. I would like to retrieve the result of pregel in ArangoDB. Here is a simple example in arangosh.
var pregel = require("#arangodb/pregel");
var params = {source: "Country/Tunesia"};
var execution = pregel.start("sssp", "CountryGraph", params);
In the document, it dumps the result of pregel in a field in the collection. Is there an alternative way like the following code to retrieve the result? I look for it in the document but didn't find it. By the way, you can load the dataset example used by the above code by loading this. Thanks.
var result = pregel.getResult(execution);

The result can not be returned directly. You have two options however:
Let ArangoDB write the Pregel result into the documents
Don't store the result, but only hold it temporarily in memory
To avoid persisting the result, there is an option store which you have to set to false. You can access the volatile result with AQL through the function PREGEL_RESULT(<handle>).
The flow is like this:
Start Pregel execution
Check status and wait until it changes to "done" or "canceled"
Perform an AQL query to access the result if Pregel succeeded
var pregel = require("#arangodb/pregel");
var params = {source: "Country/Algeria", store: false};
var handle = pregel.start("sssp", "CountryGraph", params);
while (!["done", "canceled"].includes(pregel.status(handle).state)) {
print("waiting for result");
require("internal").wait(0.5); // TODO: make this more clever
}
var status = pregel.status(handle);
print(status);
if (status.state == "done") {
var query = db._query("FOR doc IN PREGEL_RESULT(#handle) RETURN doc", {handle: handle});
print(query.toArray());
}
The output looks like this:
shell>arangosh --javascript.execute pregel.js
waiting for result
{
"state" : "done",
"gss" : 2,
"totalRuntime" : 0.00583648681640625,
"aggregators" : {
},
"sendCount" : 1,
"receivedCount" : 1,
"vertexCount" : 10,
"edgeCount" : 8,
"code" : 200
}
[
{
"_key" : "Germany",
"result" : 9223372036854776000
},
{
"_key" : "Switzerland",
"result" : 9223372036854776000
},
{
"_key" : "Brasil",
"result" : 9223372036854776000
},
{
"_key" : "Marocco",
"result" : 9223372036854776000
},
{
"_key" : "Argentina",
"result" : 9223372036854776000
},
{
"_key" : "Austria",
"result" : 9223372036854776000
},
{
"_key" : "Algeria",
"result" : 0
},
{
"_key" : "Uruguay",
"result" : 9223372036854776000
},
{
"_key" : "Tunesia",
"result" : 1
},
{
"_key" : "Australia",
"result" : 9223372036854776000
}
]

Related

Mongodb DB query to compare the data from two collections and get the results if data is same

I have two collections data like below . I need compare the two collections data and get the data if the data is same . Tried with below query but is it not giving any results .I am new to mongodb query ,Can anyone help me how to get the code key values if the data is same in both collections .The query is not returning any errors but the code values are not getting populated to new cllection
**Collection 1:**
{
"_id" : {
"value" : "12345"
},
"value" : {
"Identifier" : "12345",
"statu" : "simple",
"code" : {
"key" : "US",
"value" : "United State of America"
},
"Code" : {
"key" : "UK",
"value" : "London"
}
}
**Collection 2** :
{
"_id" : ObjectId("12345"),
"value" : {
"Identifier" : "12345",
"statu" : "simple",
"code" : {
"key" : "US",
"value" : "United State of America"
},
"Code" : {
"key" : "UK",
"value" : "London"
}
},
}
Mongo DB :
var identifiers = db.getSiblingDB("Datbase").getCollection("Collection1").find({
$or:[
{'code':{$exists:true}},
{'Code1':{$exists:true}},
]
}).toArray()
var bulk = db.getSiblingDB("Database2").getCollection("Collection2").initializeUnorderedBulkOp()
identifiers.forEach(Identifier =>{
db.getSiblingDB("Database2").getCollection("Collection2").aggregate([
{
$match:{
"Identifier":'$Identifier',
}
},
{
"$group" : {
"_id" : {
"Identifier":'$Identifier',
"key" : "$code.key",
"value" : "$code.value",
"key1" : "$code1.key1",
"value1" : "$code1.value2",
}
}
}, {
$merge:{
into:{
db:'Database',
coll:'Collection'
}
}
}
])
})
Using cursor get one by one items from 1st collection
For every item from 1st collection query 2nd collection for existense
If item does not exist or it's body is different (Use lodash.isEqual method) push them to some array or use replaceOne method to insert or replace document. (install lodash: npm i --save lodash)
const _ = require('lodash'); // import it at top
(async () => { // You can remove it if Your code already inside async scope
const source = db.getSiblingDB("Datbase").getCollection("Collection1");
const destination = db.getSiblingDB("Datbase").getCollection("Collection2");
const query = {$or:[
{'code':{$exists:true}},
{'Code1':{$exists:true}},
]};
const cursor = await source.find(query);
const differentItems = [];
// iterating source collection items
while(await cursor.hasNext()) {
const src = await cursor.next(); // source collectio inem
const queryDestination = {
"Identifier": src.Identifier, // extend Your equality constrains here
};
const dest = await destination.findOne(queryDestination);
// it may not exist || may be different (not equal)
const isDifferent = !dest || !(
_.isEqual(
_.omit(src, ['_id']),
_.omit(dest, ['_id'])
)
);
if (isDifferent) {
differentItems.push({src, dest});
/* or replace in db
await destination.replaceOne(
queryDestination,
_.omit(src, ['_id']),
{
upsert: true,
}
);
*/
}
}
console.log(`found ${differentItems.length} different documments`);
console.log(JSON.stringify(differentItems, null, 4));
)();

Querying Mongo Database

I need to perform a query on the following entry in my mongo database:
{
"_id" : ObjectId("597b19512a5b1c3258e6440e"),
"fdata" : [
{
"type" : "header",
"subtype" : "h1",
"label" : "Date Of Commencement"
},
{
"type" : "paragraph",
"subtype" : "p",
"label" : "The partnership business shall be deemed to have commenced on and from :"
},
{
"type" : "date",
"label" : "Date Field",
"description" : "Enter correct date as per instructions in the clause",
"className" : "form-control",
"name" : "date-1501239537753"
},
{
"type" : "button",
"subtype" : "submit",
"label" : "Next",
"className" : "btn btn-primary",
"name" : "button-1501239595350",
"style" : "primary"
}
]
}
I have to fetch the entire array called fdata and the parameter available to me is the value of label in an array called cl as so:
cl = ['Date Of Commencement'];
My question is how do I access that array entry as the following code gives me a 'unexpected token'
My query is like this:(using NodeJS in the backend)
for(var i=0; i<cl.length ;i++) {
var dummy = cl[i];
//console.log(dummy);
result[i] = db.collection('clauses').findOne({fdata[0].label: dummy},{fdata});
}
Its in a loop because in most cases the array cl will have multiple entries.
The findOne query you're using is asynchronous. This is why you see [ Promise { <pending> } ].
There are a lot of approaches to handle asynchronous behavior:
callbacks
async library
Promises
generators + coroutines
async + await
If your Nodejs version is new enough (8+) it should support async/await approach.
async function doJob(arr) {
const clauses = db.collection('clauses');
for (const key of arr) {
const result = await clauses.findOne({'fdata.label': key});
console.log(result);
}
}

Using MongoDB/NodeJS, how can I increment by the number of documents modified in an update query?

I have written an update query in MongoDB/NodeJS that deletes objects from an array of a document, based on the parameters I define. After I pull these objects, I would like to to increment another variable in the document based on how many documents were modified by the update query.
Here is an example of one of my events documents:
{
"_id" : ObjectId("575ed7fca7b89bb4027dded9"),
"dateCreated" : "6/13/2016",
"latitude" : "56.294786195890076",
"longitude" : "-43.59161567687988",
"instructorName" : "Test User",
"instructorEmail" : "test#user.com",
"instructorRating" : 5,
"eventName" : "We gon exercise",
"eventDescription" : "We gon exercise",
"spacesAvailable" : 15,
"streetAddress" : "123 wer",
"city" : "rty",
"state" : "NY",
"zip" : "12332",
"date" : "06/21/2016",
"startTime" : "12:00",
"endTime" : "02:10",
"tags" : [
"Cardio",
"Crossfit"
],
"price" : 5,
"attendies" : [
{
"_id" : ObjectId("5759cfcdb71d80fb2d1203ef"),
"name" : "Buddy Loester",
"email" : "Bud18#gmail.com",
"timeStamp" : 1467048318510,
"payed" : true
},
{
"_id" : ObjectId("574f257b05086e2c7f7940ca"),
"name" : "Trainer Trainer",
"email" : "trainer#user.com",
"timeStamp" : 1467055627894,
"payed" : true
}
],
"unpayed" : 0
}
Here is my code to give a better visualization:
var eventCollection = req.db.get('events');
// get current time since epoch in milliseconds
var milliSinceEpoch = new Date().getTime();
eventCollection.update(
{"attendies.payed" : {$eq : false}},
{
$pull:
{
"attendies" : {"timeStamp": {$lt: milliSinceEpoch /*- 600000*/}}
},
$inc:
{
spacesAvailable: numberAffected
}
},
{
multi: true
}, function(err, numberAffected) {
console.log(numberAffected);
return res.end();
}
);
If I specify 'numberAffected' in the query portion to '1', then it works as expected and increments by 1. However, I would like to increment by the number affected.
I know this code will not work with 'numberAffected' in the query. Using 'numberAffected' in the callback actually does return the number of documents modified by my query.
Does there exist a way in MongoDB to do what I am trying to do?
I have solved my problem by rewriting the query. It is as follows:
var ObjectID = require("mongodb").ObjectID;
var eventCollection = req.db.get('events');
var milliSinceEpoch = new Date().getTime();
// find and return all the documents in the events DB where there is a user who has not payed for an event
// they RSVP'd for
eventCollection.find({"attendies.payed" : {$eq : false}}, function(err, documentsWithUnpayedUsers) {
// if error finding, print it and return
if(err) {
console.log(err);
return res.sendStatus(400, "Error cancelling");
}
// if everyone has payed for all RSVP'd events
if(!documentsWithUnpayedUsers) return res.sendStatus(404, "Everyone has payed!");
// loop through every document which has people who have not yet payed for RSVP'd events
for(var i = 0; i < documentsWithUnpayedUsers.length; i++) {
// for each of these documents:
eventCollection.update(
{_id: ObjectID(documentsWithUnpayedUsers[i]._id)},
{
// remove the user from the attendie list if they have not payed,
// and it has been 10 minutes since they RSVP'd
$pull:
{
"attendies" : {"timeStamp": {$lt: milliSinceEpoch - 600000}, "payed" : {$eq : false}}
},
// then modify the number of spaces available for the event by the number of people who were
// removed from the attendie list
// then modify the amount of people who have not payed for the event yet (will now be 0)
$inc:
{
spacesAvailable: documentsWithUnpayedUsers[i].unpayed,
unpayed: -documentsWithUnpayedUsers[i].unpayed
}
}, function(err) {
// error checking for the update query
if(err){
console.log(err);
return res.sendStatus(400, "There was an error removing an attendie fom the event: "
+ documentsWithUnpayedUsers[i].eventName);
}
}
); // end of update query
} // end of for loop
return res.end();
}
); // end of find()
}); // end of checkPayed

How to get the max with a where condition in this situation Mongoose / Node

I am working with Mongoose, I have a collection that has documents like this
{
"_id" : 1,
"body" : "[{\"order_id\":\"647936\",\"order_datetime\":\"2015-12-02 11:10:00\"}]",
"user_info" : {
"contact_email" : "test#test.com",
"contact_phone" : "1234567",
},
"type" : "ORDERS",
"version" : 1
}
{
"_id" : 2,
"body" : "[{\"order_id\":\"647936\",\"order_datetime\":\"2015-12-02 11:10:00\"}]",
"user_info" : {
"contact_email" : "test#test.com",
"contact_phone" : "1234567",
},
"type" : "ORDERS",
"version" : 2
}
{
"_id" : 3,
"body" : "[{\"order_id\":\"647936\",\"order_datetime\":\"2015-12-02 11:10:00\"}]",
"user_info" : {
"contact_email" : "test#test.com",
"contact_phone" : "1234567",
},
"type" : "ORDERS",
"version" : 3
}
As you can see in body field you can see the order_id , so same order_id can be repeated in multiple in documents but the version will be different.
What I want is I want to search for the maximum version number for a
given order_id .
In my case it would be 3 .
I tried to use simple queries like
myCollection.aggregate([
{ "$match" : { "body.order_id" : 647936 } },
{ "$group": {
"_id" :"version",
"max": { "$max": "version" }
}}
] , function(err, data){
console.log(err);
console.log(data);
});
But the result is
null
[]
** Note that my mongoose connection is working fine and I can do some simple queries and results are OK.
Your data is the problem here since what seems to be intended as a structured document has been stored as a string:
// Bad bit
"body" : "[{\"order_id\":\"647936\",\"order_datetime\":\"2015-12-02 11:10:00\"}]",
Instead you would want this:
// Acutally data and not a string
"body" : [{ "order_id": "647936", "order_datetime": ISODate("2015-12-02 11:10:00.000Z" }],
With data like that, getting the latest version is a simple matter of ordering the results, without the overhead of .aggregate():
myCollection.find({ "body.order_id": "647936" })
.sort({ "version": -1 }).limit(1).exec(function(err,result) {
})
No need to aggregate and it's much faster than doing so, as you are just picking out the document with the latest (largest) version number.
In order to "fix" the data you can do something like this as a "one shot" execution in the shell:
var bulk = db.myCollection.initializeOrderedBulkOp(),
count = 0;
// query selects just those "body" elements that are currently a string
db.myCollection.find({ "body": { "$type": 2 } }).forEach(function(doc) {
var fixBody = JSON.parse(doc.body); // Just parse the string
fixBody.forEach(function(el) {
// Fix dates
el.order_datetime = new Date(
Date.parse(el.order_datetime.split(" ").join("T") + "Z")
);
});
// And queue an update to overwrite the "body" data
bulk.find({ "_id": doc._id }).updateOne({
"$set": { "body": fixBody }
});
count++;
// Send every 1000
if ( count % 1000 == 0 ) {
bulk.execute();
bulk = db.myCollection.initializeOrderedBulkOp(),
}
});
// Send any remaining batched
if ( count % 1000 != 0 )
bulk.execute();
You might also want to convert those "strings" other than the date to numeric values in a similar fashion and change appropriately in the query.

Fetch sub-document Mongodb only match with criteria

I have data in mongodb like this:
{
"_id" : ObjectId("55a12bf6ea1956ef37fe4247"),
"tempat_lahir" : "Paris",
"tanggal_lahir" : ISODate("1985-07-10T17:00:00.000Z"),
"gender" : true,
"family" : [
{
"nama" : "Robert Deniro",
"tempat_lahir" : "Bandung",
"tanggal_lahir" : ISODate("2015-07-09T17:00:00.000Z"),
"pekerjaan" : "IRT",
"hubungan" : "XXX",
"tanggungan" : false,
"_id" : ObjectId("55a180f398c9925299cb6e90"),
"meta" : {
"created_at" : ISODate("2015-07-11T20:59:25.242Z"),
"created_ip" : "127.0.0.1",
"modified_at" : ISODate("2015-07-12T15:54:39.682Z"),
"modified_ip" : "127.0.0.1"
}
},
{
"nama" : "Josh Groban",
"tempat_lahir" : "Jakarta",
"tanggal_lahir" : ISODate("2015-06-30T17:00:00.000Z"),
"pekerjaan" : "Balita",
"hubungan" : "Lain-Lain",
"tanggungan" : true,
"_id" : ObjectId("55a29293c65b144716ca65b2"),
"meta" : {
"created_at" : ISODate("2015-07-12T16:15:15.675Z"),
"created_ip" : "127.0.0.1"
}
}
]
}
when i try to find data in sub-document, with this code:
person.findOne({ _id: req.params.person, {'family.nama': new RegExp('robert', 'gi') }}, function(err, data){
// render code here
});
It show all data in Family Data,
Can we fetch or display a data only match with criteria/keyword, for example only "Robert Deniro" row
Thank You
In 'regular' MongoDB, you can use the $ operator for that. I'm not sure if it works with Mongoose, but it's worth a try:
person.findOne({
_id : req.params.person,
'family.nama' : new RegExp('robert', 'gi')
}, {
// Only include the subdocument(s) that matched the query.
'family.$' : 1
}, function(err, data){
// render code here
});
If you need any of the properties from the parent document (tempat_lahir, tanggal_lahir or gender; _id will always be included), you need to add them to the projection object explicitly.
One caveat: the $ operator will only return the first matching document from the array. If you need it to return multiple documents, you can't use this method and (AFAIK) have to postprocess the results after they are returned from the database.
It solved with this code:
var options = {
family: {
$elemMatch: { nama: req.query.keyword }
},
};
person.findOne({ _id: req.params.person, 'family.nama': keyword }, options, function(err, data){
//render code here
});
Thanks to #hassansin & #robertklep

Resources