Mongodb equivalent to map reduce - mongodb

Hello I want to query with adding and subtraction on mongodb aggregation
This is what I do right now,
//schema for image
{
votes : {type:Number},
comments : {type :Array},
name : {type:String}
}
var o = {};
o.map = function () { emit(this._id, (4*(parseInt(this.votes)))+(3*(parseInt(this.comments.length)))+(0*(parseInt(this.views)))) }(parseInt(this.comments.length)))))/(parseInt(currentDate-this.created_at.getDate())+1) }
o.reduce = function (k,v) { return parseInt(v); }
o.out = { replace: 'trending_images' }
o.verbose = true;
Image.mapReduce(o, function (err, model, stats) {
console.log('map reduce took %d ms', stats.processtime)
model.find({created_at:{
$gte : weekBack,
$lt:today
}}).sort('-value').limit(limit).skip(skip).exec(function (err, docs) {
async.mapSeries(docs,function (doc,cb){
cb(null,doc._id);
},function (err,ids) {
console.log("Trending mapreduce",JSON.stringify(ids),ids);
return cb({_id:{$in:ids}})
})
});
});
how do I make something similar in Aggregation framework?
UPDATE : This is what I tried.
Image.aggregate(
{$project : {
_id : 1,
trending : {
total_votes : {$multiply:["$votes", 4]},
total_comments : {$multiply:["$comments.length", 3]},
rank : {$sum:["$total_comments","$total_votes"]}
}
} },
{$match:{created_at:{
$gte : weekBack,
$lt:today
}}
},
{$sort : {rank:-1} }
,
function (err,images) {
console.log(err,images);
}
)
Not sure if this works because, it raises an error, [MongoError: no such cmd: aggregate]
I checked the version for mongodb and mongoosejs, mongodb is version MongoDB shell version: 2.4.1
mongoosejs is version 3.6.4
But still that error! what else I can try?

Related

How to use aggregation function mongo db-query

I am new in MongoDB and I would like to use the aggregation function where I want to check type == topic and get the following output
Expected output
[
{
conceptName : 59d98cfd1c5edc24e4024d00
totalCount : 2
},
{
conceptName : 59d98cfd1c5edc24e4024d03
totalCount : 1
}
]
Sample input db.GroupContents
{
"_id" : "5a0948bb1c5edc7a5000521a",
"type" : "topic",
"groupID" : "5a0948bb1c5edc7a5000521a",
"pedagogyID" : "59d98cfa1c5edc24e40249a3",
}
Sample input db.PedagogyNodes
{
"_id" : "59d98cfa1c5edc24e40249a3",
"latestVersion" : "59d98cfa1c5edc24e402497f_1",
"createdAt" : "2017-10-08 04:27:06",
"updatedAt" : "2017-10-08 04:27:06"
}
Sample input db.PedagogyVersions
{
"_id" : "59d98cfa1c5edc24e402497f_1",
"type" : "topic",
"contentNodes" : {
"LearningNodes" : [
"59d98cfd1c5edc24e4024d00",
"59d98cfd1c5edc24e4024d03",
"59d98cfd1c5edc24e4024d00",
]
},
"createdAt" : "2017-10-08 04:27:06",
"updatedAt" : "2017-10-08 04:27:06"
}
What I have tried so far
var groupID = "5a0948bb1c5edc7a5000521a"; // Step 1
var records;
var pnDoc;
var pvDoc;
db.GroupContents.find({groupID : groupID}).forEach(function (doc){ // Step 2
var pedagogyID = doc.pedagogyID;
var records = db.getSiblingDB('PedagogyService');
records.PedagogyNodes.find({_id : pedagogyID}).forEach(function (pnDoc) { // Step 3
var latestVersion = pnDoc.latestVersion;
// addded aggregate function here
records.PedagogyVersions.aggregate([
{
$match:{_id:latestVersion} // Step 4
},
{
$unwind:"$contentNodes.LearningNodes"
},
{
$group:
{
_id:"$contentNodes.LearningNodes",
count:{$sum:1}
}
}
])
})
});
I am unable to write db query based on my expected answer, please help.
Understand my requirement
Step : 1 => I am passing `groupID = 5a0948bb1c5edc7a5000521a`
Step : 2 => we have to check from GroupContents where groupID = groupID then we have to take `pedagogyID`
Step : 3 => we have to check from PedagogyNodes where _id = pedagogyID then we have to take `latestVersion`
Step : 4 => we have to check from PedagogyVersions where _id = latestVersion then we have to take `contentNodes->LearningNodes`
Step : 5 => Finally we have to do the aggregation then we have display the result
Try to unwind the LearningNodes array and then count them by grouping them together
db.PedagogyNodes.aggregate([
{
$unwind:"$contentNodes.LearningNodes"
},
{
$group:
{
_id:"$contentNodes.LearningNodes",
count:{$sum:1}
}
}
])
In case you need to do any matches you can use the $match stage
db.PedagogyNodes.aggregate([
{
$match:{type:"topic"}
},
{
$unwind:"$contentNodes.LearningNodes"
},
{
$group:
{
_id:"$contentNodes.LearningNodes",
count:{$sum:1}
}
}
])
Answering the edited question =>
You were not able to view the output on the console since mongoshell does not print script output on the screen. To do this, do the following:
var result = records.PedagogyVersions.aggregate([......]);
result.forEach(function(resultDoc){
print(tojson(resultDoc))
})
To see the result of your aggregation you have to pass the callback to be executed as parameter.
records.PedagogyVersions.aggregate([
{
$match:{_id:latestVersion} // Step 4
},
{
$unwind:"$contentNodes.LearningNodes"
},
{
$group:
{
_id:"$contentNodes.LearningNodes",
count:{$sum:1}
}
}
], function(err, results) {
console.log(results);
});

MongoDB update only a few records, create a new attribute, assign a value

I wasn't lucky finding any help on this anywhere else.
Basically I want to update only the few documents that do not have a given attribute already.
And the value for the update comes from a field that is already on the document.
This is what I tried but it didn't like the "from a field already on the document" part. Saying Cn doesn't exist.
db.getCollection('test').update(
// query
{ "id2" : { $exists: false } },
// update
{ id2: Cn },
// options
{
"multi" : true, // update all documents
"upsert" : false // don't insert new documents
}
);
Here is my test data
/* 1 */
{
"_id" : ObjectId("5912132c4a58677726d37168"),
"Cn" : "CA",
"id2" : "CAAB",
"Prov" : "AB"
}
/* 2 */
{
"_id" : ObjectId("591213404a58677726d37172"),
"Cn" : "CA",
"id2" : "CANZ",
"Prov" : "NZ"
}
/* 3 */
{
"_id" : ObjectId("591213534a58677726d37180"),
"Cn" : "CA",
"id2" : "CAMB",
"Prov" : "MB"
}
/* 4 */
{
"_id" : ObjectId("591213674a58677726d3718c"),
"Cn" : "US"
}
/* 5 */
{
"_id" : ObjectId("591213894a58677726d371a3"),
"Cn" : "MX"
}
All this should do is create a id2 on US & MX and give those new id2 attributes the corresponding values 'US' & 'MX'.
This would not be a big deal but I have able 144 countries & 10,000+ documents to add id2 to.
You can't update multiple items with an operator that only contains expressions, and self referencing doesn't work either.
Check out this question for work around solutions to get what you need done: Update MongoDB field using value of another field
You can try this :
db.your_collection.aggregate( [
{ $match : { id2 : { $exists: false }}},
{ $addFields: {
id2 : "$Cn"
}
},
{ $out : "your_collection" }
]);
This will remove all documents in collection which have id2 field.
You can do this in mongoose in a longer way :
db.getCollection('test').find({ "id2" : { $exists: false } }, function(err, docs){
if(err){
//handle errror
}
else if(!docs){
// no docs found
}
else{
for(var i=0; i< docs.length; i++){
db.getCollection('test')
.findByIdAndUpdate(
docs[i]._id
, { $set : { id2 : docs[i].Cn}}
, { new : true}
, function(err, doc){
if(err){
//handle error
}
else{
//doc was updated
}
});
}
}
});
I would not have gotten to this without Mihir Bhende pushing. Thanks for that.
var c = db.getCollection('test').find(
{ "id2" : { $exists: false }}
);
c.forEach(function(myDoc) {
print("doc", myDoc.Cn);
db.getCollection('test').update( {_id: myDoc._id}, {$set: { "id2": myDoc.Cn }}, function (err) {
if (err) { print("err"); }
});
});

Meteor Mongodb Bulk Update by _id not working

I'm trying to perform following bulk Update Operation by _id:
let bulk = Transactions.rawCollection().initializeOrderedBulkOp();
transactions.forEach((t) => {
let query = { '_id' : new Meteor.Collection.ObjectID(t._id._str) };
bulk.find( query ).update( { $set: { balance: t.balance, updated_at: new Date() } } );
});
bulk.execute(Meteor.bindEnvironment((err, res) => {
if (!err) {
callback(null, true);
} else {
console.log("Error Ocurred");
callback(true, null);
}
}));
But This find query by _id is not working. If I do query by any other field It works just fine. But I can't do that because Collection has only one Unique field as '_id'.
EDITED:
Following is Transaction MongoDB Document
{
"_id" : ObjectId("5899f8a15d79b02f6075100d"),
"transaction" : "Charge",
"description" : "01/01/2017 - 01/28/2017",
"created_by" : "SYSTEM",
"residency_id" : "6pFs3sBMZPtp3e5N9",
"value" : 25.82,
"balance" : 25.82,
"created_at" : ISODate("2017-02-07T16:41:05.718+0000"),
"updated_at" : ISODate("2017-02-07T18:08:05.378+0000")
}
Help Needed. TIA

how to query for exact mach in unknown number of subfields in mongodb

I have a collection where documents can have an unknown number of sub documents:
"agent_id": {
"0":"1234",
"1":"2234",...etc
How do I search for an exact match in all the agent_id sub-fields?
You need to dynamically create an object with properties that are a concatenation of the embedded document name agent_id with the dot (.) and the field name, enclosed in quotes, something like this:
var query = {
"agent_id.0": "78343",
"agent_id.1": "78343",
"agent_id.2": "78343",
"agent_id.3": "78343",
...
"agent_id.n": "78343"
}
One way to create the object is generate the sub-documents keys with mapReduce. The following demonstrates this approach. In the Map-Reduce operation, an array of keys in the agent_id subdocument is generated to an output collection "collection_keys" and then used to produce the find() query expression:
Suppose you populate a sample collection
db.collection.insert([
{
"agent_id": {
"0":"1234",
"1":"2234",
"56":"8451",
"74":"1475",
"10":"1234"
}
},
{
"agent_id": {
"5":"5874",
"18":"2351"
}
}
])
Running the following mapReduce operation
var mr = db.runCommand({
"mapreduce" : "collection",
"map" : function() {
for (var key in this.agent_id) { emit(key, null); }
},
"reduce" : function(key, stuff) {
return null
},
"out": "collection" + "_keys"
});
var query = { "$or": [] },
value = "1234";
db[mr.result].distinct("_id").forEach(function (key){
var obj = {};
obj["agent_id." + key] = value;
query["$or"].push(obj)
});
printjson(query);
will produce:
{
"$or" : [
{
"agent_id.0" : "1234"
},
{
"agent_id.1" : "1234"
},
{
"agent_id.10" : "1234"
},
{
"agent_id.18" : "1234"
},
{
"agent_id.5" : "1234"
},
{
"agent_id.56" : "1234"
},
{
"agent_id.74" : "1234"
}
]
})
You can then use the query document in your find() query:
db.collection.find(query)
which will produce the result:
/* 0 */
{
"_id" : ObjectId("561d5312cd05efc95a1ea1f4"),
"agent_id" : {
"0" : "1234",
"1" : "2234",
"56" : "8451",
"74" : "1475",
"10" : "1234"
}
}

Upserting on embedded document

I have the following document strucutre
{
"_id" : "NmBYYasdsa",
"objectId" : "asdsd"
"text" : "test",
....
"publishedAt" : ISODate("2015-05-28T15:31:51Z"),
"updatedAt" : ISODate("2015-05-28T15:31:51Z"),
"data" : {
.....
"likeCount" : 0,
"replyCount" : 0
}
}
That is use to synchronise my database with an external API. To do this, I poll the API once every minute and do a bulk upsert, matching on the object id to keep my database up to date.
Problem is that the data subdocument doesn't get updated when upserting, any ideas as to why?
My bulkwrite method
Mongo.Collection.prototype.upsertBulk = function(matcher, documents, options) {
if (_.isEmpty(documents)) { throw Error('Empty list of documents provided'); }
options = options || {};
var operations = documents.map(function(_document) {
_document._id = Random.id();
var operation = {
updateOne: {
filter: {},
update: { $set: _document },
upsert: true
},
};
operation['updateOne']['filter'][matcher] = _document[matcher];
return operation;
});
this.__mongoCollection(function(collection) {
collection.bulkWrite(operations, options, function(error, result) {
if (error) { throw error; }
return result;
});
});
};