Object built-in object

ObjectMongoCollection

mongodb database dataset object

useMongoDB.getCollectioncreate:

1 2
var col1 = mdb.getCollection('test'); var col = mdb.test;

inheritance relationship

operator

operator[String]

Quickly get the collection object of the current collection sub-namespace in the form of attributes

1
readonly MongoCollection MongoCollection[String];

return result:

  • return new collection object

member function

find

Create a cursor object according to the given query conditions and return field settings

1 2
MongoCursor MongoCollection.find(Object query = {}, Object projection = {});

Call parameters:

  • query: Object, the object specifying the query condition
  • projection: Object, specifies the object to return the field

return result:


findOne

According to the given query conditions and return field settings, query a result

1 2
Object MongoCollection.findOne(Object query = {}, Object projection = {});

Call parameters:

  • query: Object, the object specifying the query condition
  • projection: Object, specifies the object to return the field

return result:

  • Object, returns the first result

findAndModify

query and modify

1
Object MongoCollection.findAndModify(Object query);

Call parameters:

  • query: Object, specify query conditions and modify data

return result:

  • Object, return the result and other information before modification

insert

Insert a set of data

1
MongoCollection.insert(Array documents);

Call parameters:

  • documents: Array, specifies the data array to be inserted

Insert a piece of data

1
MongoCollection.insert(Object document);

Call parameters:

  • document: Object, specifies the data to be inserted

save

Save a piece of data, if the data contains the _id field, it is an update, otherwise it is an insert

1
MongoCollection.save(Object document);

Call parameters:

  • document: Object, specifies the data to save

update

Update data based on given query conditions

1 2 3 4
MongoCollection.update(Object query, Object document, Boolean upsert = false, Boolean multi = false);

Call parameters:

  • query: Object, the object specifying the query condition
  • document: Object, specifies the data to update
  • upsert: Boolean, when the data does not exist, insert a new data, the default is false, do not insert
  • multi: Boolean, when more than one piece of data meets the condition, update all data, the default is false, only update the first one

Update data based on given query conditions

1 2 3
MongoCollection.update(Object query, Object document, Object options);

Call parameters:

  • query: Object, the object specifying the query condition
  • document: Object, specifies the data to update
  • options: Object, upsert and multi options passed as object fields

remove

Delete data based on given query conditions

1
MongoCollection.remove(Object query);

Call parameters:

  • query: Object, the object specifying the query condition

runCommand

Execute database commands

1
Object MongoCollection.runCommand(Object cmd);

Call parameters:

  • cmd: Object, the given command object

return result:

  • Object, returns the command to return the result

Execute database commands

1 2
Object MongoCollection.runCommand(String cmd, Object arg = {});

Call parameters:

  • cmd: String, the given command name
  • arg: Object, given command parameter options

return result:

  • Object, returns the command to return the result

drop

delete current collection

1
MongoCollection.drop();

ensureIndex

Create an index on the current collection

1 2
MongoCollection.ensureIndex(Object keys, Object options = {});

Call parameters:

  • keys: Object, given index field, order and direction
  • options: Object, options for a given index, unique index, etc.

reIndex

Rebuild the index of the current collection

1
Object MongoCollection.reIndex();

return result:

  • Object, returns the command execution result

dropIndex

Delete the index of the specified name in the current collection

1
Object MongoCollection.dropIndex(String name);

Call parameters:

  • name: String, given the name of the index to delete

return result:

  • Object, returns the command execution result

dropIndexes

Delete all indexes of the current collection

1
Object MongoCollection.dropIndexes();

return result:

  • Object, returns the command execution result

getIndexes

Query all indexes of the current collection

1
Array MongoCollection.getIndexes();

return result:

  • Array, returns a result set containing the index

getCollection

Get the collection object of the current collection subnamespace

1
MongoCollection MongoCollection.getCollection(String name);

Call parameters:

  • name: String, subnamespace name

return result:

  • MongoCollection, returns a new collection object

toString

Return the string representation of the object, generally return "[Native Object]", the object can be reimplemented according to its own characteristics

1
String MongoCollection.toString();

return result:

  • String, returns a string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable properties defined by the object

1
Value MongoCollection.toJSON(String key = "");

Call parameters:

  • key: String, not used

return result:

  • Value, which returns a JSON-serializable value