Mongdb basic operation and usage of java

All data exists in BSON (similar JSON) Mongdb format, the set may be stored, map, binary files, and other data types.

Common database operations

use [database name]; // have to select, add and select not to 
show dbs; // query all database list
db; // view the current library
db.dropDatabase (); // delete the current library;

Common set of operations

show collectoins; // set of all queries in the current library; 
Show the Tables; // is the set of all queries in the current library;
. DB [collection name] .drop (); // set to delete the specified name; 

DB [collection name] .insert ({});. // if the presence of the collection, will create and add data.

Common Operations for the

. DB [collection name] .insert ({}); // add the data set for the specified name; 
DB [collection name] .insertOne ({});. // add a data set for the specified name;
DB [collection name. ] .insertMany ({}); // add to the set of a plurality of data specified name;

DB [collection name] .find ();. // query specifies a set of all the data;
DB [collection name] .findOne ();. / / query specifies the first data set exists;
DB [collection name] .findAndModify ();. // query and modify all data
DB [collection name] .findOneAndDelete ();. // first check out the compliance data condition and delete
db [collection name] .findOneAndReplace ();. // query the data in line with the conditions of the first and replace
db [collection name] .findOneAndUpdate ();. // check out the first qualifying and modify data

. db [collection name] .deleteOne (); // delete a qualified data 
db [collection name] .deleteMany ();. // delete the data meet the requirements of multiple

db [collection name] .update ().; // modify a set of qualified data
DB [collection name] .updateOne ();. // modify data that meets a set of criteria
DB [collection name] .updateMany ();. // modify the set of all qualified data

Queries operator:

Gt greater than $, $ lt less than, greater than or equal GTE $, $ lte less.

db.test.find({"t":{"$gt":10}});
db.test.find({"t":{"$lt":10}});
db.test.find({"t":{"$gte":10}});
db.test.find({"t":{"$lte":10}});

Modify the operator needs to be added when the document:

$inc

The value of this field based on the value to be added incrementally.

db.test.update ({ "a": "d"}, { "$ inc": { "t": 2}}); // field of numeric type can be used

$ i

Value is multiplied by the value of the specified field

db.test.update ({ "a": "d"}, { "$ mul": { "t": 3}}); // field of numeric type can be used

$rename

Renaming Fields

db.test.update({"a":"d"},{"$rename":{"a":"b"}});

$ setOnInsert  

In operation, the operation assigned to the corresponding field

// When the data when the condition is not detected, the add operation, the value will be performed after the $ setOnInsert, provided that the parameters set for upsert to true 
// When the data already exists in isolated conditions, will perform $ set after the statement
// $ setOnInsert use $ set together under normal circumstances.
db.test.update({"a":"d"},{"$setOnInsert":{"as":"df"},"$set":{"zzx":"sdf"}},{"upsert":"true"});

$set

To specify the value of a bond, if it does not exist, create

db.test.update ({ "a": " b"}, { "$ set": { "a": "c"}}); // all the values of a = b to a C; If there are a plurality find qualified, will modify the first data to be inserted; 
db.test.update ({ "a": "B"}, { "$ SET": { "Time": ISODate ( "2019-11-12 12:34:56")}}); // add time field

$unset

To specify the value of a bond, if it does not exist is not created created

db.test.update ({ "a": "b"}, { "$ unset": { "a": [arbitrary parameter]}}); // delete the specified field is understood to mean

$

Only when smaller than the conventional field values ​​specified update the field.

db.test.update ({ "z": " s"}, { "$ min": { "t": 14}}); // must be completed with a value less than the stored value to be updated;

$max

Only when the value is greater than the current specified field values ​​to update the field.

db.test.update ({ "z": " s"}, { "$ max": { "t": 14}}); // must be completed with a value greater than the stored value to be updated;

 

The basic operation of the java Mongdb

Necessary to introduce the following jar package

 

 

 1. Connect mongodb

// initialize mongdb database, mongdb default port is initialized 27017 
public
class the DataBase { Private static MongoClient mongoClient; static { System.out.println ( "MongoDB initialization" ); IF (mongoClient == null ) { mongoClient = new new MongoClient ( "localhost ", 27017 ); } } }

2. Connect the following specified collection specified database

   public static MongoCollection<Document> getCollection(String dbName, String collName) {
        if (null == collName || "".equals(collName)) {
            return null;
        }
        if (null == dbName || "".equals(dbName)) {
            return null;
        }
        MongoCollection<Document> collection = mongoClient.getDatabase(dbName).getCollection(collName);
        return collection;
    }

3. Follow

Add (insertOne can be replaced insertMany ())

MongoCollection<Document> collection = getCollection(dbName, collName);
Document param = new Document();
param.put("a", "b");
param.put("c", "d");
collection.insertOne(param);

Modification (updateOne updateMany can be replaced or findOneAndUpdate)

MongoCollection<Document> coll = getCollection("test", "login");
Document test = new Document();
test.put("a","v");
test.put("b","z");
coll.updateOne(test, new Document("$set",new Document("role","user")));

Delete (deleteOne can be replaced deleteMany or findOneAndDelete )

MongoCollection<Document> collection = getCollection(dbName, collName);
Document param = new Document();
param.put("a", "b");
param.put("c", "d");
collection.deleteONe(param);

Inquire

MongoCollection<Document> collection = getCollection(dbName, collName);
Document param = new Document();
param.put("a", "b");
param.put("c", "d");
FindIterable<Document> find = collection.find(param);
MongoCursor<Document> iterator = find.iterator();
while (iterator.hasNext()) {
  Document next = iterator.next();
  System.out.println(next);
}

Guess you like

Origin www.cnblogs.com/pastjx/p/12099110.html