Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

hey guys how to write this mongoDB syntax to java

db.users.find( { user_id: /bc/ },{user_name:/bc/},{age:/2/} )

my source is

BasicDBObject sortOrder = new BasicDBObject();
MongoClient mongoClient;
DB db;

DBCollection table;
DBCursor cursor = null;

mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDB("stackoverflow");
boolean auth = db.authenticate("aku","kamu".toCharArray());
table = db.getCollection("questions");
cursor = table.find();

while (cursor.hasNext()) {
    DBObject object = cursor.next();
    out.print(object.get("title"));
    answer = rn.nextInt(8) + 0;
}

any solution guys? i am newbie on using mongoDB

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
195 views
Welcome To Ask or Share your Answers For Others

1 Answer

    DBObject idMatch = new BasicDBObject("user_id","bc");
    DBObject usernameMatch = new BasicDBObject("user_name",bc);
    DBObject ageMatch = new BasicDBObject("age",2);

    DBObject andAll = new BasicDBObject("$and", Arrays.asList(existence, firstNotMatch, secondNotMatch));

    //calling  table.find will get you what you wanted.
    table.find(andAll);

If you want to 'OR' the conditions, just replace $and with $or. note that the above code is not tested properly and you may need to modify it a bit to make it work.

Your question is not clear, but i hope i helped.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...