MongoDB Aggregate query in Java [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am new to Java and like to know how to build mongoDB query in java.
db.coll.aggregate(
{ $match : {
loc: {
"$ref" : "location",
"$id" : ObjectId("4fe69610e7e9fa378c3c802e")
}
}},
{ $unwind : "$ActivityList" },
{ $match : {
'ActivityList.user': {
"$ref" : "userProfile",
"$id" : ObjectId("4fdeafe1de26fd298262bb82")
}
}},
{ $group : {
_id : "$ActivityList.type",
latest: { $max: '$ActivityList.timestamp' }
}}
);
Thanks for your help.

There is limitation in aggregate command, pipeline can't operate on values of Binary, Symbol, MinKey, MaxKey, DBRef, Code, CodeWScope. Check Aggregation Framework for more information.

Related

Removing the object from nested json using its key and value (Java script or jQuery) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to remove the one object from json data....My JSON would be like this
This is my JSON data:
[
{
"comp2": [
{
"Create_SecurityGroup1_Securitygroup_name": {
"description": "sg"
}
}
]
},
{
"comp1": [
{
"Create_Keypair1_Keypair_name": {
"default": "kp"
}
}
]
}
]
I need to remove "comp1" or "comp2" datas ...this key values are dynamic...but structure is same...
Once i removed the key from this JSON data ...My result would like this if i remove "comp2"..
[
{
"comp1": [
{
"Create_Keypair1_Keypair_name": {
"default": "kp"
}
}
]
}
]
Please help me to solve this issue ....
if you want to remove data from json object then u can use the slice method which work for json and array.
var recentActdata = [
{
"displayValue":"Updated Guidelines",
"link":"#",
"timestamp":"29/06/2013 01:32"
},
{
"displayValue":"Logging",
"link":"#",
"timestamp":"28/06/2013 16:19"
},
{
"displayValue":"Subscribe",
"link":"#",
"timestamp":"21/06/2013 14:30"
}]
$.each(recentActdata.slice(0,5), function(i, data) {
var ul_data = "<li><h3>"+ data.displayValue+ "</h3></li>";
$("#recentActivities").append(ul_data);
});
Here is a demo example you can see the use of slice
use following link
http://jsfiddle.net/enXcn/1/
If you have control over the JSON, your format is a little odd. There's really no reason to put every element inside an array. This would be plenty valid and accomplish the same...
var json = { "comp2": {
"Create_SecurityGroup1_Securitygroup_name": {
"description": "sg"
}
}
},
{ "comp1": {
"Create_Keypair1_Keypair_name": {
"default": "kp"
}
}
}
with this format, you simply use...
delete json["comp2"];
If you do not have control of your format, then you'll need to access the 1st element in the array first...
delete json[0]["comp2"];

Get all field values java program [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am building a Tetris game. I am currently debugging the game and in order to do this I need to see the values of all the variables and the variables variables and so on. With reflection I can get all a classes fields by doing this:
try
{
for(Field field : this.getClass().getDeclaredFields())
{
field.setAccessible(true);
System.out.println(field.get(this));
}
}
catch(Exception e)
{
}
What I don't know how to get all the field values of each field object.
There are two things you need to do:
Create a set of reachable objects. You don't want to recursively traverse your object graph forever.
Print values for every object.
For the first one, you need to use something like IdentityHashMap:
import java.util.IdentityHashMap;
class MyObjectCache
{
final IdentityHashSet objects = new IdentityHashSet ();
...
}
To traverse objects you can use recursive function (it is simpler, but has a stack restriction):
class MyObjectCache
{
....
void registerObject(Object o)
{
if (objects.contains(o))
{
return;
}
objects.add(o);
for(Field field : o.getClass().getDeclaredFields())
{
field.setAccessible(true);
registerObject(field.get(o));
}
}
...
}
And then you can start printing collected objects...

Why do I get this error when trying to read integers from a file and copy them into an array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a text file named "hours.txt" that has lines of integers that I would like to read and copy them into an array.
The integers are the number of hours worked by 8 employees in a week. So I created a two-dimensional array with the rows being the employees and the columns being the days of the week.
public static void read()
{
Scanner read = new Scanner(new File("hours.txt"));
int[][] hours = new int[8][7];
for(int r=0; r<hours.length; r++)
{
for(int c=0; c<hours[0].length; c++)
{
while(read.hasNextInt())
{
hours[r][c]= read.nextInt();
}
}
}
}
When I try to compile this, I get the following error:
EmployeeHours.java:16: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Why is that?
Because FileNotFoundException is a checked exception. You must either catch and handle it, or throws it in the method declaration. And don't just swallow the exception; that's almost never the right way to "handle" them.
Lots more reading on exactly this topic can be found in the official Java Tutorial.
try {
//block of code
} catch (FileNotFoundException fnfe) {
}
or
public static void read() throws FileNotFoundException
The exception FileNotFoundException must be declared as part of your method signature, to tell the Java compiler that your method can throw that particular exception. You must change your method definition to:
public static void read() throws FileNotFoundException
{
... code here ...
}

Breaking out of a method in a loop [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a method with a break statement in its if statement. The method is in a while loop. Will it break out of the while loop if I use break within the method's if statement or do I have to use nested loops?
public int x=0;public int y=0;
public boolean endCondition = true;
public void someMethod()
{
if(x!=y) {//do something}
else break;
}
while(endCondition==true)
{
this.someMethod();
}
System.out.println("Bloke");
You can not use break without a loop or a switch . You need to use return. But it seems a endless method calling which would cause StackOverflow exception.
To break out from a function you have to use return. break will break you out from only the inner loop inside which you are calling it.
You probably need to return a boolean value from the method, which you can then use to decide whether to break the loop.
It's not important in this simple example, but it's usually a good idea to label your loops when using break, so it's clear what you are breaking out of, especially when using nested loops. See the label FOO below.
public boolean someMethod()
{
if(x!=y)
{
//do something
return false;
}
return true; // break
}
FOO:while(true)
{
if(someMethod()) break FOO;
}

What does (String args []) mean? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have this in the File.java:
public static void main(String args[]) throws Exception_Exception {
URL wsdlURL = CallSService.WSDL_LOCATION;
if (args.length > 0) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
and I want to transfer this to a JSP file, so I do like that:
List<String> Search(String keyS){
if(keyS!=null){
QName SERVICE_NAME = new QName("http://ts.search.com/", "callSService");
String arg=??????????????;
URL wsdlURL = CallSService.WSDL_LOCATION;
File wsdlFile = new File(arg);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(arg);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
I want to replace the args[0] with arg. What does (String args[]) mean? and how can I replace it ?
String args[] is an array of Strings passed in from the command line.
So, if you started your app with java MyApp arg1 arg2
Then args[] would contain => ["arg1", "arg2"]
Java will automatically split up arguments separated by spaces, which is how it knows how many arguments you passed in.
Don't do this in a JSP :(
Don't put your functionality in a main, it's confusing: public static void main is conventionally a program's entry point, not a general purpose method. You may use it as one, but IMO it is misleading.
Instead, create an instance method you can call with the argument you want. It could be a static method, but this builds in some inflexibility making things more difficult to test. Embeddeding the code in a JSP also increases testing difficulty.
You'll need to use ServletContext.getRealPath() to get a file relative to the web app, unless you're providing an absolute path. If the file is "embedded" in the app (on the classpath) you'll want to use one of the resourceAsStream variants.

Categories

Resources