Package com.mongodb

Examples of com.mongodb.DBCollection


  }

  private void dropAllCollections(DB db) {
    Set<String> collectionNames = db.getCollectionNames();
    for (String name : collectionNames) {
      DBCollection collection = db.getCollection(name);
      try {
        collection.drop();
      } catch (RuntimeException e) {
        // ignore
      }
    }
  }
View Full Code Here


  @Test
  public void shouldCreateNewObjectInEmbeddedMongoDb() {

    // Given
    DBCollection col = db.createCollection("testCollection", new BasicDBObject());

    // When
    col.save(new BasicDBObject("testDoc", new Date()));

    // Then
    assertThat(col.getCount()).isEqualTo(1L);
  }
View Full Code Here

                final Resource rsrc = this.getResource(resolver, path, info);
                if ( rsrc instanceof MongoDBResource ) {
                    this.deletedResources.add(path);
                    this.changedResources.remove(path);

                    final DBCollection col = this.getCollection(info[0]);
                    final String pattern = "^" + Pattern.quote(info[1]) + "/";

                    final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
                    final DBCursor cur = col.find(query);
                    while ( cur.hasNext() ) {
                        final DBObject dbObj = cur.next();
                        final String childPath = info[0] + '/' + dbObj.get(getPROP_PATH());
                        this.deletedResources.add(childPath);
                        this.changedResources.remove(childPath);
View Full Code Here

        try {
            for(final String deleted : this.deletedResources) {
                final String[] info = this.extractResourceInfo(deleted);

                // check if the collection still exists
                final DBCollection col = this.getCollection(info[0]);
                if ( col != null ) {
                    if ( col.findAndRemove(QueryBuilder.start(getPROP_PATH()).is(info[1]).get()) != null ) {
                        this.context.notifyRemoved(info);
                    }
                }
            }
            for(final MongoDBResource changed : this.changedResources.values()) {

                final DBCollection col = this.context.getDatabase().getCollection(changed.getCollection());
                if ( col != null ) {
                    final String[] info = new String[] {changed.getCollection(),
                            changed.getProperties().get(getPROP_PATH()).toString()};
                    // create or update?
                    if ( changed.getProperties().get(PROP_ID) != null ) {
                        col.update(QueryBuilder.start(getPROP_PATH()).is(changed.getProperties().get(getPROP_PATH())).get(),
                                changed.getProperties());
                        this.context.notifyUpdated(info);
                    } else {
                        // create
                        col.save(changed.getProperties());
                        this.context.notifyUpdated(info);
                    }
                } else {
                    throw new PersistenceException("Unable to create collection " + changed.getCollection(), null, changed.getPath(), null);
                }
View Full Code Here

                        throw new UnsupportedOperationException("remove");
                    }

                };
            }
            final DBCollection col = this.getCollection(info[0]);
            if ( col != null ) {
                final String pattern;
                if ( info.length == 1 ) {
                    pattern = "^([^/])*$";
                } else {
                    pattern = "^" + Pattern.quote(info[1]) + "/([^/])*$";
                }

                final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
                final DBCursor cur = col.find(query).
                        sort(BasicDBObjectBuilder.start(getPROP_PATH(), 1).get());
                return new Iterator<Resource>() {

                    public boolean hasNext() {
                        return cur.hasNext();
View Full Code Here

                return new MongoDBCollectionResource(resourceResolver, path);
            }
            return null;
        }
        logger.debug("Searching {} in {}", info[1], info[0]);
        final DBCollection col = this.getCollection(info[0]);
        if ( col != null ) {
            final DBObject obj = col.findOne(QueryBuilder.start(getPROP_PATH()).is(info[1]).get());
            logger.debug("Found {}", obj);
            if ( obj != null ) {
                return new MongoDBResource(resourceResolver,
                        path,
                        info[0],
View Full Code Here

        {
            return null;
        }
        Iterator<Resource> returnValue = null;
        final String collectionName = query.substring( 0, query.indexOf( ".find(" ) );
        DBCollection col = this.getCollection( collectionName );
        if ( col != null )
        {
            String criteria = query.trim().substring( query.indexOf( ".find(" ) + 6, query.length() - 1 );
            DBObject dbObject = (DBObject) JSON.parse( criteria );
            final DBCursor cur = col.find( dbObject );
            final String rootPath = context.getRootWithSlash();
           
            return new Iterator<Resource>() {

                public boolean hasNext() {
View Full Code Here

     * <p>
     * Note that currently only one {@code DBObject} is supported per collection, but this could be expanded into a
     * more general mechanism if required.
     */
    public MockMongoClientBuilderContext insert(String collectionName, DBObject object) {
      DBCollection collection = mock( DBCollection.class );
      collections.put( collectionName, collection );

      when( collection.findOne( any( DBObject.class ), any( DBObject.class ), any( ReadPreference.class ) ) ).thenReturn( object );

      WriteResult writeResult = mock( WriteResult.class );
      when( collection.remove( any( DBObject.class ), any( WriteConcern.class ) ) ).thenReturn( writeResult );

      return this;
    }
View Full Code Here

     * Builds and returns a mock MongoDB client based on the given configuration.
     */
    public MockMongoClient build() {
      DB database = mock( DB.class );

      DBCollection defaultCollection = mock( DBCollection.class );
      when( database.getCollection( anyString() ) ).thenReturn( defaultCollection );

      for ( Entry<String, DBCollection> collection : collections.entrySet() ) {
        when( database.getCollection( collection.getKey() ) ).thenReturn( collection.getValue() );
      }
View Full Code Here

    /**
     * Returns the collection with a given name. This is a Mockito mock object, so verifications can be performed on
     * it.
     */
    public DBCollection getCollection(String collectionName) {
      DBCollection collection = collections.get( collectionName );
      return collection != null ? collection : defaultCollection;
    }
View Full Code Here

TOP

Related Classes of com.mongodb.DBCollection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.