Java MongoDB, consultas.

En este tutorial, muestra algunas rutinas en Java para  obtener documentos de la colección NoSQL en MongoDB

package com.mvit.contexto;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoApp {
public static void InertDocument(DBCollection collection) {

List<DBObject> list = new ArrayList<DBObject>();

Map<String, String> env = System.getenv();
int i = 1;
for (String envName : env.keySet()) {
BasicDBObject data = new BasicDBObject();
data.append("number", i);
data.append("variable", envName +"=" + env.get(envName));
data.append("date", new Date());
list.add(data);
i++;
}

collection.insert(list);
}

public static void main(String[] args) {

try {

Mongo mongo = new Mongo("127.0.0.1", 27017);
DB db = mongo.getDB("contexto");

// get a single collection
DBCollection collection = db.getCollection("enviroment");

InertDocument(collection);

System.out.println("1. Encuentra el primer elemento");
DBObject dbObject = collection.findOne();
System.out.println(dbObject);

System.out.println("\n2. Obtener todos los elementos de la collection");
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}

System.out.println("\n3. Obtener el campo variable ");
BasicDBObject allQuery = new BasicDBObject();
BasicDBObject fields = new BasicDBObject();
fields.put("variable", 1);

DBCursor cursor2 = collection.find(allQuery, fields);
while (cursor2.hasNext()) {
System.out.println(cursor2.next());
}

System.out.println("\n4. Encontrar el valor con number = 5");
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("number", 5);
DBCursor cursor3 = collection.find(whereQuery);
while (cursor3.hasNext()) {
System.out.println(cursor3.next());
}

System.out.println("\n5. Encontrar el valor con number en 2,4 and 5");
BasicDBObject inQuery = new BasicDBObject();
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(4);
list.add(5);
inQuery.put("number", new BasicDBObject("$in", list));
DBCursor cursor4 = collection.find(inQuery);
while (cursor4.hasNext()) {
System.out.println(cursor4.next());
}

System.out.println("\n6. Encontrar el valor con 5 > number > 2");
BasicDBObject gtQuery = new BasicDBObject();
gtQuery.put("number", new BasicDBObject("$gt", 2).append("$lt", 5));
DBCursor cursor5 = collection.find(gtQuery);
while (cursor5.hasNext()) {
System.out.println(cursor5.next());
}

System.out.println("\n7. Encontrar el valor con number != 4");
BasicDBObject neQuery = new BasicDBObject();
neQuery.put("number", new BasicDBObject("$ne", 4));
DBCursor cursor6 = collection.find(neQuery);
while (cursor6.hasNext()) {
System.out.println(cursor6.next());
}

System.out.println("\n8. Encontrar el valor con number = 2 and name = 'PROCESSOR_LEVEL = 6' ");
BasicDBObject andQuery = new BasicDBObject();

List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("number", 2));
obj.add(new BasicDBObject("variable", "PROCESSOR_LEVEL=6"));
andQuery.put("$and", obj);

System.out.println(andQuery.toString());

DBCursor cursor7 = collection.find(andQuery);
while (cursor7.hasNext()) {
System.out.println(cursor7.next());
}

System.out.println("\n9. Encontrar el valor con name = 'Sys.*-[1-3]', case sensitive example");
BasicDBObject regexQuery = new BasicDBObject();
regexQuery.put("variable",
new BasicDBObject("$regex", "Sys.*-[1-3]")
.append("$options", "i"));

System.out.println(regexQuery.toString());

DBCursor cursor8 = collection.find(regexQuery);
while (cursor8.hasNext()) {
System.out.println(cursor8.next());
}

collection.drop();

System.out.println("Fin");

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}

}
}

 

 

 

 

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s