Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

JNoSQL/Diana/MongoDB

< JNoSQL‎ | Diana
Revision as of 02:56, 17 November 2017 by Unnamed Poltroon (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

MongoDB

MongoDB (from humongous) is a free and open-source cross-platform document-oriented database program


Maven Project

To use ArandoDB driver in a Maven Project:

        <dependency>
            <groupId>org.jnosql.diana</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>version</version>
        </dependency>

E.g:

        <dependency>
            <groupId>org.jnosql.diana</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>0.0.1</version>
        </dependency>


Document

The MongoDB document implementation uses the MongoDBDocumentConfiguration to the configuration. Beyond the method also it can read a configuration from diana-mongodb.properties file.


  • mongodb-server-host-: as prefix to add host client, eg: mongodb-server-host-1=host1, mongodb-server-host-2= host2


Sample Code


public class App {

    public static final String DATABASE = "database";
    public static final String DOCUMENT_COLLECTION = "person";

    public static void main(String[] args)  {
        DocumentConfiguration configuration = new MongoDBDocumentConfiguration();
        try(DocumentCollectionManagerFactory collectionFactory = configuration.get();) {
            DocumentCollectionManager collectionManager = collectionFactory.get(DATABASE);

            DocumentEntity entity = DocumentEntity.of(DOCUMENT_COLLECTION);
            entity.add(Document.of("name", "Daniel Soro"));
            entity.add(Document.of("age", 26));

            DocumentEntity entitySaved = collectionManager.save(entity);
            Optional<Document> id = entitySaved.find("_id");

            DocumentQuery query = select.from(DOCUMENT_COLLECTION).where(DocumentCondition.eq(id.get());
            List<DocumentEntity> documentsFound = collectionManager.select(query);


        }
    }
}

Links

Back to the top