Example usage for com.mongodb.client MongoCollection insertOne

List of usage examples for com.mongodb.client MongoCollection insertOne

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection insertOne.

Prototype

void insertOne(TDocument document);

Source Link

Document

Inserts the provided document.

Usage

From source file:if4031.ServerHandler.java

@Override
public boolean saveToDB(String token, String channel, String message) throws TException {
    MongoCollection<Document> messageCollection = database.getCollection(channel + "Message");
    int lastId;//from w ww . jav  a2 s  . c  om
    //        System.out.println(this.getLastMessageId(channel));
    //        Document listMessage = new Document("messages",new Document("id", 3).append("nick", token).append("messsage", message));
    //        Document updateQuery = new Document("$push", listMessage);
    //        Document findQuery = new Document("name", channel);
    //        channelCollection.updateOne(findQuery, updateQuery);

    //        Document messageDoc = new Document("id", this.getLastMessageId(channel)).append("nick", token).append("message", message);
    //        messageCollection.insertOne(messageDoc);

    try {
        lastId = this.getLastMessageId(channel);
        lastId++;
    } catch (Exception e) {
        lastId = 1;
    }
    Document doc = new Document("id", lastId).append("nick", token).append("message", message);
    messageCollection.insertOne(doc);
    return true;
}

From source file:if4031.ServerHandler.java

@Override
public String saveNick(String nick) throws TException {
    MongoCollection<Document> userCollection = database.getCollection("User");
    Document doc = new Document("nick", nick);
    userCollection.insertOne(doc);
    return nick;//www.  j a v  a 2 s.c o  m
}

From source file:if4031.ServerHandler.java

@Override
public String createChannel(String channel) throws TException {
    MongoCollection<Document> channelCollection = database.getCollection("Channel");

    /* channel not exist */
    Document doc = new Document("name", channel);

    channelCollection.insertOne(doc);

    return "Channel berhasil dibuat";
}

From source file:it.terrinoni.m101j.spark.HelloWorldMongoDBSparkFreemarkerStyle.java

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(HelloWorldMongoDBSparkFreemarkerStyle.class, "/freemarker");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("course");
    final MongoCollection<Document> collection = database.getCollection("hello");

    collection.drop();/*from w  w w . ja  v a  2 s  .  c  om*/

    collection.insertOne(new Document("name", "MongoDB"));

    Spark.get("/", new Route() {
        @Override
        public Object handle(Request request, Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template helloTemplate = configuration.getTemplate("hello.ftl");

                Document document = collection.find().first();

                helloTemplate.process(document, writer);

            } catch (IOException | TemplateException ex) {
                Spark.halt(500);
                ex.printStackTrace();
            }
            return writer;
        }
    });
}

From source file:it.terrinoni.Question.java

public static void main(String[] args) {
    MongoClient c = new MongoClient();
    MongoDatabase db = c.getDatabase("test");
    MongoCollection<Document> animals = db.getCollection("animals");

    Document animal = new Document("animal", "monkey");

    animals.insertOne(animal);

    animal.remove("animal");
    animal.append("animal", "cat");
    try {/*from  ww w. j  a va 2s  .  c  o  m*/
        animals.insertOne(animal);
    } catch (MongoWriteException ex) {
        System.err.println(animal.getString("animal") + " not inserted");
    }

    animal.remove("animal");
    animal.append("animal", "lion");
    try {
        animals.insertOne(animal);
    } catch (MongoWriteException ex) {
        System.err.println(animal.getString("animal") + " not inserted");
    }

    long tot = animals.count();

    System.out.println("Total number of inserted animals: " + tot);
}

From source file:javamongo.TweetMethod.java

public void tweet(String text) {
    String uuid = UUID.randomUUID().toString();
    //        UUID timeuuid = UUIDs.timeBased();

    // insert into tweets
    MongoCollection<Document> collectionTweets = db.getCollection("tweets");
    Document docTweets = new Document("tweet_id", uuid).append("username", username).append("body", text);
    collectionTweets.insertOne(docTweets);

    // insert into userline
    MongoCollection<Document> collectionUserline = db.getCollection("userline");
    Document docUserline = new Document("username", username).append("tweet_id", uuid);
    collectionUserline.insertOne(docUserline);

    // insert into timeline
    MongoCollection<Document> collectionTimeline = db.getCollection("timeline");
    Document docTimeline = new Document("username", username).append("tweet_id", uuid);
    collectionTimeline.insertOne(docTimeline);

    // insert to all follower's timeline
    MongoCollection<Document> collectionFollower = db.getCollection("followers");
    try ( // find all document
            MongoCursor<Document> cursor = collectionFollower.find(eq("username", username)).iterator()) {
        while (cursor.hasNext()) {
            String follower = cursor.next().getString("followers");
            MongoCollection<Document> followerTimeline = db.getCollection("timeline");
            Document docFollower = new Document("username", follower).append("tweet_id", uuid);
            followerTimeline.insertOne(docFollower);
        }/* w w w .jav  a 2s . c o m*/
    }
}

From source file:javamongo.TweetMethod.java

public void follow(String friend) {
    Date date = new Date();

    if (getUser(friend)) {
        // insert into friends
        MongoCollection<Document> collectionFriends = db.getCollection("friends");
        Document docFriend = new Document("username", username).append("friend", friend).append("since",
                date.getTime());/*from w  w w. ja  va  2  s . c  om*/
        collectionFriends.insertOne(docFriend);

        // insert into followers
        MongoCollection<Document> collectionFollowers = db.getCollection("followers");
        Document docFollowers = new Document("username", friend).append("followers", username).append("since",
                date.getTime());
        collectionFollowers.insertOne(docFollowers);
    } else {
        System.out.println("Username doesn't exist");
    }
}

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public boolean saveUser(User user) {
    if (!GenericUserManager.shouldSave(user)) {
        user.getIoLock().lock();/*from   w  ww  . j  a v  a 2  s  .  c o m*/
        try {
            return call(() -> {
                MongoCollection<Document> c = database.getCollection("users");
                return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged();
            }, false);
        } finally {
            user.getIoLock().unlock();
        }
    }

    user.getIoLock().lock();
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("users");
            try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) {
                if (!cursor.hasNext()) {
                    c.insertOne(fromUser(user));
                } else {
                    c.replaceOne(new Document("_id", user.getUuid()), fromUser(user));
                }
            }
            return true;
        }, false);
    } finally {
        user.getIoLock().unlock();
    }
}

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public boolean createAndLoadGroup(String name) {
    Group group = plugin.getGroupManager().getOrMake(name);
    group.getIoLock().lock();//  w w w.jav a  2  s  .c  o m
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("groups");

            try (MongoCursor<Document> cursor = c.find(new Document("_id", group.getName())).iterator()) {
                if (cursor.hasNext()) {
                    // Group exists, let's load.
                    Document d = cursor.next();
                    group.setNodes(revert((Map<String, Boolean>) d.get("perms")));
                } else {
                    c.insertOne(fromGroup(group));
                }
            }
            return true;
        }, false);
    } finally {
        group.getIoLock().unlock();
    }
}

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public boolean createAndLoadTrack(String name) {
    Track track = plugin.getTrackManager().getOrMake(name);
    track.getIoLock().lock();//from   ww  w .j a  va2s .c om
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("tracks");

            try (MongoCursor<Document> cursor = c.find(new Document("_id", track.getName())).iterator()) {
                if (!cursor.hasNext()) {
                    c.insertOne(fromTrack(track));
                } else {
                    Document d = cursor.next();
                    track.setGroups((List<String>) d.get("groups"));
                }
            }
            return true;
        }, false);
    } finally {
        track.getIoLock().unlock();
    }
}