db.posts.aggregate([{$unwind:"$comments"},{$group:{"_id":"$comments.author",total_comments:{$sum:1}}},{$sort:{"total_comments":-1}},{$limit:1}])
Saturday, June 28, 2014
Saturday, June 7, 2014
Write a program in the language of your choice that will remove the grade of type "homework" with the lowest score for each student from the dataset that you imported in HW 2.1. Since each document is one grade, it should remove one document per student.
package com.tengen;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
public class Homework23 {
public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB db = client.getDB("students");
DBCollection collection = db.getCollection("grades");
BasicDBObject bdb = new BasicDBObject("type", "homework");
BasicDBObject sort = new BasicDBObject("student_id", 1).append("score",
1);
// DBCursor cur = collection.find(bdb).sort(sort);
List<DBObject> pipeline = new ArrayList<DBObject>();
pipeline.add(new BasicDBObject("$group", new BasicDBObject("_id",
"$student_id")));
AggregationOutput aoup = collection.aggregate(pipeline);
Iterable<DBObject> results = aoup.results();
Iterator<DBObject> itr = results.iterator();
DBCursor cur = null;
try {
while (itr.hasNext()) {
DBObject agdb = itr.next();
bdb.append("student_id", agdb.get("_id"));
cur = collection.find(bdb).sort(sort);
cur.hasNext();
DBObject docToRemove = cur.next();
Double minScore = (Double) docToRemove.get("score");
while (cur.hasNext()) {
DBObject doc1 = cur.next();
if (minScore > (Double) doc1.get("score")) {
minScore = (Double) doc1.get("score");
docToRemove = doc1;
}
}
System.out.println("*********************");
System.out.println(docToRemove);
System.out.println("*********************");
collection.remove(docToRemove);
}
}
finally {
if (cur != null)
cur.close();
}
}
}
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
public class Homework23 {
public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB db = client.getDB("students");
DBCollection collection = db.getCollection("grades");
BasicDBObject bdb = new BasicDBObject("type", "homework");
BasicDBObject sort = new BasicDBObject("student_id", 1).append("score",
1);
// DBCursor cur = collection.find(bdb).sort(sort);
List<DBObject> pipeline = new ArrayList<DBObject>();
pipeline.add(new BasicDBObject("$group", new BasicDBObject("_id",
"$student_id")));
AggregationOutput aoup = collection.aggregate(pipeline);
Iterable<DBObject> results = aoup.results();
Iterator<DBObject> itr = results.iterator();
DBCursor cur = null;
try {
while (itr.hasNext()) {
DBObject agdb = itr.next();
bdb.append("student_id", agdb.get("_id"));
cur = collection.find(bdb).sort(sort);
cur.hasNext();
DBObject docToRemove = cur.next();
Double minScore = (Double) docToRemove.get("score");
while (cur.hasNext()) {
DBObject doc1 = cur.next();
if (minScore > (Double) doc1.get("score")) {
minScore = (Double) doc1.get("score");
docToRemove = doc1;
}
}
System.out.println("*********************");
System.out.println(docToRemove);
System.out.println("*********************");
collection.remove(docToRemove);
}
}
finally {
if (cur != null)
cur.close();
}
}
}
What is the student_id of the lowest exam score above 65?
For "What is the student_id of the lowest exam score above 65?"
run the below query you will get the answer
db.grades.aggregate({$group:{_id:{studentId:"$student_id",'typ1':"$type"},'average':{$avg:'$score'}}},{"$sort":{"average":1}},{$match:{"_id.typ1":"exam","average":{$gte:65}}},{"$limit":1});
run the below query you will get the answer
db.grades.aggregate({$group:{_id:{studentId:"$student_id",'typ1':"$type"},'average':{$avg:'$score'}}},{"$sort":{"average":1}},{$match:{"_id.typ1":"exam","average":{$gte:65}}},{"$limit":1});
Thursday, May 29, 2014
Kill a Process running on a specific port in windows OS (i tested in windows 8.1)
1 . Find which process is running on the specific port with the below command
netstat -ano
2 . Then take the PID (which in my case is in the last column) , then run the following command
taskkill /F /PID <<pid>>
once you run the above command and if the process killed successfully then you will see the below message
SUCCESS: The process with PID <<pid>> has been terminated.
* <<>pid>> represents the pid which is using the specific port number.
netstat -ano
2 . Then take the PID (which in my case is in the last column) , then run the following command
taskkill /F /PID <<pid>>
once you run the above command and if the process killed successfully then you will see the below message
SUCCESS: The process with PID <<pid>> has been terminated.
* <<>pid>> represents the pid which is using the specific port number.
Sunday, May 18, 2014
JAVA NIO , BUFFER , LIMIT and POSITION
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileNIO {
public static void main(String[] args) throws IOException {
try {
FileOutputStream fout = new FileOutputStream("nio.txt");
FileChannel fc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocate(10);
bb.clear();
for (int i = 0; i < 10; i++) {
System.out.println("space left in the buffer "+bb.limit());
System.out.println("next element where the data need to be put :"+bb.position());
bb.put((byte) 'A');
}
System.out.println("space left in the buffer "+bb.limit());
System.out.println("next element where the data need to be put :"+bb.position());
bb.flip();
fc.write(bb);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileNIO {
public static void main(String[] args) throws IOException {
try {
FileOutputStream fout = new FileOutputStream("nio.txt");
FileChannel fc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocate(10);
bb.clear();
for (int i = 0; i < 10; i++) {
System.out.println("space left in the buffer "+bb.limit());
System.out.println("next element where the data need to be put :"+bb.position());
bb.put((byte) 'A');
}
System.out.println("space left in the buffer "+bb.limit());
System.out.println("next element where the data need to be put :"+bb.position());
bb.flip();
fc.write(bb);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
will print the following output
space left in the buffer 10
next element where the data need to be put :0
space left in the buffer 10
next element where the data need to be put :1
space left in the buffer 10
next element where the data need to be put :2
space left in the buffer 10
next element where the data need to be put :3
space left in the buffer 10
next element where the data need to be put :4
space left in the buffer 10
next element where the data need to be put :5
space left in the buffer 10
next element where the data need to be put :6
space left in the buffer 10
next element where the data need to be put :7
space left in the buffer 10
next element where the data need to be put :8
space left in the buffer 10
next element where the data need to be put :9
space left in the buffer 10
next element where the data need to be put :10
Thursday, May 15, 2014
JSTL Cookie
To access the cookie using the jstl in jsp
{cookie.cookieName.value} will return the value
{cookie.cookieName.value} will return the value
Sunday, September 22, 2013
Subscribe to:
Posts (Atom)