MongoDBÖÐObjectIdµÄÎóÇø£¬ÒÔ¼°ÒýÆðµÄһϵÁÐÎÊÌâ

來源:互聯網
上載者:User

MongoDBÖÐObjectIdµÄÎóÇø£¬ÒÔ¼°ÒýÆðµÄһϵÁÐÎÊÌâ

½üÆÚ¶ÔÁ½¸öÓ¦ÓýøÐиÄÔ죬ÔÚÉÏÏß¹ý³ÌÖгöÏÖһϵÁÐÎÊÌ⣨ÆäÖÐÒ»²¿·ÖÊÇÓÉÓÚObjectIdÎóÇøµ¼Öµģ

ÏÈÀ´Á˽âÏÂObjectId£º

TimeStamp<†·Ÿ"http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPiA8L2gzPsewIDTOu8rH0ru49nVuaXi1xMqxvOS0waOsysfSu7j2aW50wOCx8KOsztLDx72ryc/D5rXEwP3X09bQtcRvYmplY3RpZLXEx7A0zru9+NDQzOHIoaGwNGRmMmRjZWOhsaOsyLu689TZvavL+8PHsLLXsMquwfm9+NbGINeozqrKrr341sajuqGwMTMwNzc2MTkwMKGxo6zV4rj2yv3X1r7NysfSu7j2yrG85LTBo6zOqsHLyMPQp7n7uPy80cP3z9SjrM7Sw8e9q9XiuPbKsbzktMHXqru7s8nO0sPHz7C537XEyrG85Ljxyr2jqDxzdHJvbmc+vqvIt7W9w+s8L3N0cm9uZz4pPGJyIC8mZ3Q7JCBkYXRlIC1kIA=="1970-01-01 UTC 1307761900 sec' -u
Machine
½ÓÏÂÀ´µÄÈý¸ö×Ö½Ú£¬¾ÍÊÇ 2cdcd2 ,ÕâÈý¸ö×Ö½ÚÊÇËùÔÚÖ÷»úµÄΨһ±êʶ·û£¬Ò»°ãÊÇ»úÆ÷Ö÷»úÃûµÄÉ¢ÁÐÖµ£¬ÕâÑù¾ÍÈ·±£Á˲»Í¬Ö÷»úÉú³É²»Í¬µÄ»úÆ÷hashÖµ£¬È·±£ÔÚ·Ö²¼Ê½Öв»Ôì³É³åÍ»£¬ÕâÒ²¾ÍÊÇÔÚͬһ̨»úÆ÷Éú³ÉµÄobjectidÖмäµÄ×Ö·û´¶¼ÊÇһģһÑùµÄÔÒò¡£
pid ÉÏÃæµÄMachineÊÇΪÁËÈ·±£ÔÚ²»Í¬»úÆ÷²úÉúµÄobjectid²»³åÍ»£¬¶øpid¾ÍÊÇΪÁËÔÚͬһ̨»úÆ÷²»Í¬µÄmongodb½ø³Ì²úÉúÁËobjectid²»³åÍ»£¬½ÓÏÂÀ´µÄ0936Á½Î»¾ÍÊDzúÉúobjectidµÄ½ø³Ì±êʶ·û¡£
increment Ç°ÃæµÄ¾Å¸ö×Ö½ÚÊDZ£Ö¤ÁËÒ»ÃëÄÚ²»Í¬»úÆ÷²»Í¬½ø³ÌÉú³Éobjectid²»³åÍ»£¬ÕâºóÃæµÄÈý¸ö×Ö½Úa8b817£¬ÊÇÒ»¸ö×Ô¶¯Ôö¼ÓµÄ¼ÆÊýÆ÷£¬ÓÃÀ´È·±£ÔÚͬһÃëÄÚ²úÉúµÄobjectidÒ²²»»á·¢ÏÖ³åÍ»£¬ÔÊÐí256µÄ3´Î·½µÈÓÚ16777216Ìõ¼Ç¼µÄΨһÐÔ¡£
ÂÒÐòµÄ¡£
ÂÒÐòµÄ¡£
Query query = new Query();if (id != null){query.addCriteria(Criteria.where("_id").gt(id));}query.with(new Sort(Sort.Direction.ASC, "_id"));
Comparator<DBObject> comparator = new Comparator<DBObject>(){@Overridepublic int compare(DBObject o1, DBObject o2){return ((ObjectId)o1.get("_id")).compareTo((ObjectId)o2.get("_id"));}};PriorityQueue<DBObject> queue = new PriorityQueue<DBObject>(200,comparator);ÎóÇø ¶þ ¡¢¶à¿Í»§¶Ë¸ß²¢·¢Ê±£¬ÊÇ·ñ¿ÉÒÔ±£Ö¤Ë³Ðò£¨sortֺ󣣿Èç¹ûÒ»Ö±±£Ö¤Ð´ÈëÔ¶Ô¶´óÓÚ¶Á³ö£¨¼ä¸ôÒ»ÃëÒÔÉÏ££¬ÕâÑùÊÇÓÀÔ¶²»»á³öÏÖÂÒÐòµÄÇé¿ö¡£

protected WriteResult insert(List<DBObject> list, boolean shouldApply , com.mongodb.WriteConcern concern, DBEncoder encoder ){ if (encoder == null) encoder = DefaultDBEncoder.FACTORY.create(); if ( willTrace() ) { for (DBObject o : list) { trace( "save: " + _fullNameSpace + " " + JSON.serialize( o ) ); } } if ( shouldApply ){ for (DBObject o : list) { apply(o); _checkObject(o, false, false); Object id = o.get("_id"); if (id instanceof ObjectId) { ((ObjectId) id).notNew(); } } } WriteResult last = null; int cur = 0; int maxsize = _mongo.getMaxBsonObjectSize(); while ( cur < list.size() ) { OutMessage om = OutMessage.insert( this , encoder, concern ); for ( ; cur < list.size(); cur++ ){ DBObject o = list.get(cur); om.putObject( o ); // limit for batch insert is 4 x maxbson on server, use 2 x to be safe if ( om.size() > 2 * maxsize ){ cur++; break; } } last = _connector.say( _db , om , concern ); } return last; }×Ô¶¯Ìí¼ÓObjectIdµÄ²Ù×÷
/** * calls {@link DBCollection#apply(com.mongodb.DBObject, boolean)} with ensureID=true * @param o <code>DBObject</code> to which to add fields * @return the modified parameter object */ public Object apply( DBObject o ){ return apply( o , true ); } /** * calls {@link DBCollection#doapply(com.mongodb.DBObject)}, optionally adding an automatic _id field * @param jo object to add fields to * @param ensureID whether to add an <code>_id</code> field * @return the modified object <code>o</code> */ public Object apply( DBObject jo , boolean ensureID ){ Object id = jo.get( "_id" ); if ( ensureID && id == null ){ id = ObjectId.get(); jo.put( "_id" , id ); } doapply( jo ); return id; }¿ÉÒÔ¿´µ½£¬mongoDBµÄÇý¶¯°üÖÐÊÇ»á×Ô¶¯Ìí¼ÓObjectIdµÄ¡£saveµÄ·½·¨
public WriteResult save( DBObject jo, WriteConcern concern ){ if ( checkReadOnly( true ) ) return null; _checkObject( jo , false , false ); Object id = jo.get( "_id" ); if ( id == null || ( id instanceof ObjectId && ((ObjectId)id).isNew() ) ){ if ( id != null && id instanceof ObjectId ) ((ObjectId)id).notNew(); if ( concern == null ) return insert( jo ); else return insert( jo, concern ); } DBObject q = new BasicDBObject(); q.put( "_id" , id ); if ( concern == null ) return update( q , jo , true , false ); else return update( q , jo , true , false , concern ); }
²»ÊDz»ÉèÖþÍÓÉ·þÎñ¶ËÉú³ÉµÄ¡£
DBObject update = new BasicDBObject("$inc", new BasicDBObject("counter", 1));DBObject query = new BasicDBObject("_id", key);DBObject result = getMongoTemplate().getCollection(collectionName).findAndModify(query, update);if (result == null){DBObject doc = new BasicDBObject();doc.put("counter", 1L);doc.put("_id", key);// insert(collectionName, doc);getMongoTemplate().save(doc, collectionName);return 1L;}return (Long) result.get("counter");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.