<private> new Collection(name, option, parent)
Do not call this class with new
from application.
use Longo.DB#createCollection
or Longo.DB#collection
method.
Parameters:
Name | Type | Argument | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name |
String |
<optional> |
'temp' | name of this collection |
||||||||||||||||||||
option |
Object |
<optional> |
config for capped collection Properties
|
|||||||||||||||||||||
parent |
DB | database instance |
Example
var db = Longo.createDB("test"); db.createCollection("score");
Extends
Methods
-
addEventListener(type, listner)
-
Parameters:
Name Type Description type
String A string representing the event type to listen for.
listner
function The object that receives a notification when an event of the specified type occurs.
- Inherited From:
- Source:
- See:
-
bind()
-
Alias for EventEmitter.addEventListener
- Inherited From:
- Source:
-
copyTo(newCollection, query, done) → {String}
-
Copies all documents from collection into newCollection.
If newCollection does not exist, MongoDB creates it.Longo.Collection.collectionCallback
will be called with the number of documents copied.
If the copy fails, it will be called with the error.Parameters:
Name Type Argument Default Description newCollection
String Name of destination Collection.
query
Object The query selection query.
done
collectionCallback <optional>
null callback for result. See
Longo.Collection.collectionCallback
Returns:
opId
- Type
- String
-
count(query) → {Cursor}
-
Returns the count of documents that would match a
Longo.Collection#find
query.Parameters:
Name Type Description query
Object The query selection query.
Returns:
cursor Cursor object
- Type
- Cursor
-
dataSize() → {Cursor}
-
Return the size of the collection.
Returns:
cursor Cursor object
- Type
- Cursor
-
dispatchEvent(type, data)
-
Parameters:
Name Type Description type
String dusoatch event type
data
Object dispatch parameter
-
drop(cb)
-
Drop collection from database.
Parameters:
Name Type Argument Description cb
function <optional>
callback
Returns:
promise if cb was null
-
emit()
-
Alias for EventEmitter.dispatchEvent
- Inherited From:
- Source:
-
find(query, projection) → {Cursor}
-
Selects documents in a collection and returns Cursor object.
Parameters:
Name Type Argument Default Description query
Object <optional>
{} Specifies selection query using query operators. To return all documents in a collection, omit this parameter or pass an empty document ({}). Specifies the fields to return using projection operators.
projection
Object <optional>
To return all fields in the matching document, omit this parameter.
Returns:
cursor Cursor object
- Type
- Cursor
-
findOne(query, projection) → {Cursor}
-
Returns one document that satisfies the specified query query. If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the memory. In capped collections, natural order is the same as insertion order.
Parameters:
Name Type Argument Default Description query
Object <optional>
{} Specifies selection query using query operators. To return all documents in a collection, omit this parameter or pass an empty document ({}). Specifies the fields to return using projection operators.
projection
Object <optional>
To return all fields in the matching document, omit this parameter.
Returns:
cursor Cursor object
- Type
- Cursor
-
insert(document) → {Cursor}
-
Inserts a document or documents into a collection. Different from MongoDB, this method does not accept second parameter for option.
Only JSON formattable elements are supported. So
function
andRegex
are not stored in the collection.Parameters:
Name Type Description document
Object | Array A document or array of documents to insert into the collection.
Returns:
cursor Cursor object
- Type
- Cursor
-
isCapped() → {Boolean}
-
Return collection is capped.
Returns:
isCapped
- Type
- Boolean
-
off()
-
Alias for EventEmitter.removeEventListener
- Inherited From:
- Source:
-
on()
-
Alias for EventEmitter.addEventListener
- Inherited From:
- Source:
-
parsist(func)
-
Save collection snapshot to LocalStorage.
If dataset in this collection changed, persisted data will be updated.Parameters:
Name Type Description func
function your error handler
- Source:
- See:
-
parsistOnce(func)
-
Save collection snapshot to LocalStorage.
Parameters:
Name Type Description func
function your error handler
- Source:
- See:
-
remove(query, justOne) → {Cursor}
-
Removes documents from a collection.
Parameters:
Name Type Argument Description query
Object Specifies deletion query using query operators. To delete all documents in a collection, pass an empty document ({}).
justOne
Boolean <optional>
To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion query.
Returns:
cursor Cursor object
- Type
- Cursor
-
removeEventListener(type, listner)
-
Parameters:
Name Type Description type
String A string representing the event type being removed.
listner
function The listener parameter indicates the EventListener function to be removed.
- Inherited From:
- Source:
- See:
-
renameCollection(The)
-
Changes the name of an existing collection This method have side effect
Parameters:
Name Type Description The
String new name of the collection.
-
save(doc) → {Cursor}
-
Updates an existing document or inserts a new document, depending on its document parameter.
Only JSON formattable elements are supported. So
function
andRegex
are not stored in the collection.Insert
If the document does not contain an _id field, then the save() method performs an insert().
During the operation, the longo will create an ObjectId and assign it to the _id field.Upsert
If the document contains an _id field, then the save() method performs an update with upsert, querying by the _id field.
If a document does not exist with the specified _id value, the save() method performs an insert.
If a document exists with the specified _id value,
the save() method performs an update that replaces all fields in the existing document with the fields from the document.Parameters:
Name Type Description doc
Object A document to save to the collection.
Returns:
cursor Cursor object
- Type
- Cursor
-
setDefaultErrorHandler(func)
-
Set your own error handler.
By default, Longo just log when some error happen.Parameters:
Name Type Description func
function your error handler
-
totalSize() → {Cursor}
-
Return the total size of the data in the collection. As same as
Longo.Collection#dataSize
Returns:
cursor Cursor object
- Type
- Cursor
-
trigger()
-
Alias for EventEmitter.dispatchEvent
- Inherited From:
- Source:
-
unbind()
-
Alias for EventEmitter.removeEventListener
- Inherited From:
- Source:
-
update(query, update, option) → {Cursor}
-
Modifies an existing document or documents in a collection.
The method can modify specific fields of existing document or documents or replace an existing document entirely,
depending on the update parameter.
By default, the update() method updates a single document.
Set the Multi Parameter to update all documents that match the query criteria.Parameters:
Name Type Argument Description query
Object The selection criteria for the update. Use the same query selectors as used in the find() method.
update
Object The modifications to apply.
option
Object <optional>
option.
Properties
Name Type Argument Default Description upsert
Boolean <optional>
false If set to true, creates a new document when no document matches the query criteria. which does not insert a new document when no match is found.
multi
Boolean <optional>
false If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.
Returns:
cursor Cursor object
- Type
- Cursor
Type Definitions
-
collectionCallback(error, result)
-
Callback structure for
Longo.Collection
Parameters:
Name Type Argument Default Description error
Longo.Error <optional>
null null when success
result
Array <optional>
null null when fail