知识点

软件工具

相关文章

更多

最近更新

更多

mongodb添加集合文档操作-使用shell和java操作mongodb文档

2019-03-16 22:05|来源: 网路

查看当前数据库中所有的集合,使用命令 show collections 或使用show tables
> use mydb;
switched to db mydb
> show tables;
student

student1

@Test
public void testShowCollection(){
    ListCollectionsIterable<Document> iterable = mongoDatabase.listCollections();
    MongoCursor<Document> mongoCursor = iterable.iterator();
    while (mongoCursor.hasNext()) {
        System.out.println(mongoCursor.next());
    }
}


创建集合

1、显示创建集合, 执行后会在数据库里新建一个空的集合
> db.createCollection('blog1');

{ "ok" : 1 }

mongoDatabase.createCollection("user1");


2、隐式创建集合
隐式创建可以使用命令 db.集合名称.insert({}),指创建集合并同时向集合中插入数据,例如:db.user.insert({name:"书生"})
> db.user.insert({name:"书生"})
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("575cf7680eeaf1846525401c"), "name" : "书生" }


插入文档

> db.foo.insert({"bar":"baz"})
WriteResult({ "nInserted" : 1 })
> db.foo.find();
{ "_id" : ObjectId("57555bcfa57bed6079269bd0"), "bar" : "baz" }
>


插入多个文档,最多只能插入48M

> db.foo.insert([{"id":1},{id:2}])
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 2,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})
> db.foo.find()
{ "_id" : ObjectId("57555bcfa57bed6079269bd0"), "bar" : "baz" }
{ "_id" : ObjectId("57555c42a57bed6079269bd1"), "id" : 1 }
{ "_id" : ObjectId("57555c42a57bed6079269bd2"), "id" : 2 }
>
@Test
public void testAddDocument() {
    MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("student1");
    //插入一条数据
    Document document = new Document();
    document.append("name", "zhangsan");
    document.append("age", 28);
    mongoCollection.insertOne(document);
    //插入多条数据
    List<Document> documentList = new ArrayList<Document>();
    Document document1 = new Document();
    document1.append("name", "lisi");
    document1.append("age", 28);
    document1.append("sex", "男");
    Document document2 = new Document();
    document2.append("name", "wangwu");
    document2.append("age", 31);
    document2.append("sex", "男");
    documentList.add(document1);
    documentList.add(document2);
    mongoCollection.insertMany(documentList);
}


相关问答

更多
  • 用过echo加管道符将命令传入mongo的命令行 echo "db.serverStatus().mem" | mongo admin -u$user -p$pw 如果是多条命令 mongo admin -u$user -p$pw < 评论0 3 0 加载更多
  • { "_id" : ObjectId("52fb2ceb1e2f8622d4228a7a"), "from" : 0, "message" : [{ "data" : "test1", "status" : true, "time" : 1 }, { "data" : "test2", "status" : true, "time" : 2 }], "to" : 1, "type" : "s" }
  • 您好. #!/bin/sh mongo WordPress --eval "show collections;db.posts.find().limit(10);" show collections db.posts.find().limit(10) 如果还有问题,可以继续追问,感谢。
  • DBCollection dbc = db.getCollection("role"); dbc.setHintFields(null); 虽然3.0以后版本中 DBCollection方法 已过时,可以将就用 直接用 $hint 操作符 db.users.find( { name: {}, $hint: { age : 1 } } ) 参考地址 https://docs.mongodb.org/manual/reference/operator/meta/hint/#metaOp._S_hint
  • 用过echo加管道符将命令传入mongo的命令行 echo "db.serverStatus().mem" | mongo admin -u$user -p$pw 如果是多条命令 mongo admin -u$user -p$pw < 评论0 0 0 加载更多
  • 要删除所有文档,请按如下所示使用BasicDBObject或DBCursor: MongoClient client = new MongoClient("10.0.2.113" , 27017); MongoDatabase db = client.getDatabase("maindb"); DBCollection collection = db.getCollection("mainCollection") BasicDBObject document = new BasicDBObject(); ...
  • 如果您使用_id属性的默认ObjectId值,则创建时间将在其中进行编码。 如ObjectID文档中所述 : ObjectId是一个12字节的BSON类型,使用以下构造: 一个4字节的值,表示自Unix纪元以来的秒数, 一个3字节的机器标识符 一个2字节的进程ID和一个3字节的计数器,以随机值开始。 您可以在ObjectId对象上调用getTimestamp()函数以获取ISODate对象创建时间的ISODate对象: 在mongo shell中: ObjectId().getTimestamp() ISO ...
  • 我不确定diagram是否是您的模型,请尝试使用您的模型,因为我没有看到您通过find or findOne方法获取文档,您可以在其中应用remove方法。 Model.remove({ _id: id}, function(err){}); 或者你也可以找到并删除: Model.findOne({_id: id}, function (error, daigram){ daigram.remove(); }); 您还可以使用最新版本: MyModel.findOneAndRemove({_id: ...