Class MongoCollection

java.lang.Object
com.codename1.backend.mongo.MongoCollection

public final class MongoCollection extends Object

One collection: the CRUD operations, as MongoDB's own commands.

Documents, filters and updates are Maps; Filters and Updates build the latter two. insertOne adds an _id of a new ObjectId to a document that has none, as the official drivers do, so the caller can read it back.

  • Method Summary

    Modifier and Type
    Method
    Description
    aggregate(List pipeline)
    The results of an aggregation pipeline, a list of stage documents.
    long
    How many documents filter matches.
    createIndex(Map keys, boolean unique)
    Creates an index over keys (for example {"email": 1}) unless one exists; unique makes a second document with the same key a MongoException.isDuplicateKey() failure.
    long
    deleteMany(Map filter)
    Deletes every matching document; returns how many were deleted.
    long
    deleteOne(Map filter)
    Deletes the first matching document; returns how many were deleted.
    void
    Drops the collection; a missing one is not an error.
    find(Map filter)
    Every document filter matches.
    find(Map filter, Map sort, Map projection, int skip, int limit)
    The documents filter matches, ordered by sort (null for none), with only the fields in projection (null for all), skipping skip and returning at most limit (zero for no limit).
    findOne(Map filter)
    The first document filter matches, or null.
    The collection's name.
    void
    insertMany(List documents)
    Inserts documents in order, stopping at the first that fails.
    insertOne(Map document)
    Inserts one document and returns its _id.
    void
    replaceOne(Map filter, Map replacement, boolean upsert)
    Replaces the first matching document, inserting it when none matches.
    long
    updateMany(Map filter, Map update)
    Applies update to every matching document; returns how many matched.
    long
    updateOne(Map filter, Map update)
    Applies update to the first matching document; returns how many matched.
    void
    upsert(Map filter, Map update)
    Applies update to the first matching document, or inserts one when none matches.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getName

      public String getName()
      The collection's name.
    • insertOne

      public Object insertOne(Map document) throws IOException
      Inserts one document and returns its _id.
      Throws:
      IOException
    • insertMany

      public void insertMany(List documents) throws IOException
      Inserts documents in order, stopping at the first that fails.
      Throws:
      IOException
    • find

      public List find(Map filter) throws IOException
      Every document filter matches.
      Throws:
      IOException
    • findOne

      public Map findOne(Map filter) throws IOException
      The first document filter matches, or null.
      Throws:
      IOException
    • find

      public List find(Map filter, Map sort, Map projection, int skip, int limit) throws IOException
      The documents filter matches, ordered by sort (null for none), with only the fields in projection (null for all), skipping skip and returning at most limit (zero for no limit).
      Throws:
      IOException
    • aggregate

      public List aggregate(List pipeline) throws IOException
      The results of an aggregation pipeline, a list of stage documents.
      Throws:
      IOException
    • countDocuments

      public long countDocuments(Map filter) throws IOException
      How many documents filter matches.
      Throws:
      IOException
    • updateOne

      public long updateOne(Map filter, Map update) throws IOException
      Applies update to the first matching document; returns how many matched.
      Throws:
      IOException
    • updateMany

      public long updateMany(Map filter, Map update) throws IOException
      Applies update to every matching document; returns how many matched.
      Throws:
      IOException
    • replaceOne

      public void replaceOne(Map filter, Map replacement, boolean upsert) throws IOException
      Replaces the first matching document, inserting it when none matches.
      Throws:
      IOException
    • upsert

      public void upsert(Map filter, Map update) throws IOException
      Applies update to the first matching document, or inserts one when none matches.
      Throws:
      IOException
    • deleteOne

      public long deleteOne(Map filter) throws IOException
      Deletes the first matching document; returns how many were deleted.
      Throws:
      IOException
    • deleteMany

      public long deleteMany(Map filter) throws IOException
      Deletes every matching document; returns how many were deleted.
      Throws:
      IOException
    • createIndex

      public String createIndex(Map keys, boolean unique) throws IOException
      Creates an index over keys (for example {"email": 1}) unless one exists; unique makes a second document with the same key a MongoException.isDuplicateKey() failure. Returns the index name.
      Throws:
      IOException
    • drop

      public void drop() throws IOException
      Drops the collection; a missing one is not an error.
      Throws:
      IOException