db.mycol.find("likes": {$gt:10}, $or: [{"by": "yiibai"}, {"title": "MongoDB Overview"}] }).pretty() // 'where likes>10 AND (by = 'yiibai' OR title = 'MongoDB Overview')' db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA) db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA}) db.COLLECTION_NAME.remove(DELLETION_CRITTERIA) db.COLLECTION_NAME.remove(DELETION_CRITERIA,1) db.mycol.find({},{"title":1,_id:0})//1表示显示该字段0不显示 db.COLLECTION_NAME.find().limit(NUMBER) db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) db.COLLECTION_NAME.find().sort({KEY:1})
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
操作 语法 例子 RDBMS 等同 Equality {:} db.mycol.find({“by”:”tutorials yiibai”}).pretty() where by = ‘tutorials yiibai’ Less Than {:{$lt:}} db.mycol.find({“likes”:{$lt:50}}).pretty() where likes < 50 Less Than Equals {:{$lte:}} db.mycol.find({“likes”:{$lte:50}}).pretty() where likes <= 50 Greater Than {:{$gt:}} db.mycol.find({“likes”:{$gt:50}}).pretty() where likes > 50 Greater Than Equals {:{$gte:}} db.mycol.find({“likes”:{$gte:50}}).pretty() where likes >= 50 Not Equals {:{$ne:}} db.mycol.find({“likes”:{$ne:50}}).pretty() where likes != 50
// To connect to mongodb server MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "test" ); boolean auth = db.authenticate(myUserName, myPassword);
获取一个集合列表
1 2 3 4
Set colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s); }
public class ConfirmDontLoseMessages { static int msgCount = 10000; final static String QUEUE_NAME = "confirm-test"; static ConnectionFactory connectionFactory;
public static void main(String[] args) throws IOException, InterruptedException { if (args.length > 0) { msgCount = Integer.parseInt(args[0]); }
connectionFactory = new ConnectionFactory();
// Consume msgCount messages. (new Thread(new Consumer())).start(); // Publish msgCount messages and wait for confirms. (new Thread(new Publisher())).start(); }
@SuppressWarnings("ThrowablePrintedToSystemOut") static class Publisher implements Runnable { public void run() { try { long startTime = System.currentTimeMillis();
The AMQP 0-9-1 connection and channel have the following lifecycle states:
open: the object is ready to use closing: the object has been explicitly notified to shut down locally, has issued a shutdown request to any supporting lower-layer objects, and is waiting for their shutdown procedures to complete closed: the object has received all shutdown-complete notification(s) from any lower-layer objects, and as a consequence has shut itself down
The AMQP connection and channel objects possess the following shutdown-related methods:
addShutdownListener(ShutdownListener listener) and removeShutdownListener(ShutdownListener listener), to manage any listeners, which will be fired when the object transitions to closed state. Note that, adding a ShutdownListener to an object that is already closed will fire the listener immediately getCloseReason(), to allow the investigation of what was the reason of the object’s shutdown isOpen(), useful for testing whether the object is in an open state close(int closeCode, String closeMessage), to explictly notify the object to shut down.
public void brokenMethod(Channel channel) { if (channel.isOpen()) { // The following code depends on the channel being in open state. // However there is a possibility of the change in the channel state // between isOpen() and basicQos(1) call ... channel.basicQos(1);//告诉RabbitMQ同一时间给一个消息给消费者 } }
处于无效状态时应该抓取异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public void validMethod(Channel channel) { try { ... channel.basicQos(1); } catch (ShutdownSignalException sse) { // possibly check if channel was closed // by the time we started action and reasons for // closing it ... } catch (IOException ioe) { // check why connection was closed ... } }
连接设置 设置pool数
1 2
ExecutorService es = Executors.newFixedThreadPool(20); Connection conn = factory.newConnection(es);
使用地址列表
1 2 3
Address[] addrArr = new Address[]{ new Address(hostname1, portnumber1) , new Address(hostname2, portnumber2)}; Connection conn = factory.newConnection(addrArr);
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.