如何在java中的数组mongodb中插入一个对象

时间:2018-05-21 10:49:11

标签: java mongodb

package hellomongo;
import java.util.ArrayList;

import java.util.List;
import java.util.List;
import javax.management.Query;

import java.util.List
import org.bson.types.ObjectId;

public class Update {
  private final static String HOST = "localhost";
  private final static int PORT = 27017;
  public static void main(String args[]) {

  MongoClient mongoClient = new MongoClient(HOST, PORT);
  DB db = mongoClient.getDB("admin");
  DBCollection coll = db.getCollection("command");



  ArrayList<Document> docList = new ArrayList<Document>();
  docList.add(new Document().append("d", 4));

  Document push = new Document().append(
    "$push", new Document().append("cmd", 
      new Document().append("$each", docList).append("$position", 3)));

  coll.updateMany(new  Document().append("_id","5b02a14804ed7d6c3ca1c262"), push);


}

0 个答案:

没有答案