Database Reference
In-Depth Information
db.towns.insert
function
(obj,
_
allow
_
dot) {
if
(!obj) {
throw
"no object passed to insert!"
;
}
if
(!
_
allow
_
dot) {
this
.
_
validateForStorage(obj);
}
if
(
typeof
obj.
_
id ==
"undefined"
){
var
tmp = obj;
obj = {
_
id:
new
ObjectId};
for
(
var
key
in
tmp) {
obj[key] = tmp[key];
}
}
this
.
_
mongo.insert(
this
.
_
fullName, obj);
this
.
_
lastID = obj.
_
id;
}
Wir wollen weitere Dokumente in unsere
towns
-Collection einfügen und ent-
wickeln dafür unsere eigene JavaScript-Funktion.
mongo/insert_city.js
function
insertCity(
name, population, last
_
census,
famous
_
for, mayor
_
info
){
db.towns.insert({
name:name,
population:population,
last
_
census: ISODate(last
_
census),
famous
_
for:famous
_
for,
mayor : mayor
_
info
});
}
Sie können den Code einfach in die Shell einfügen. Dann rufen Sie sie wie
folgt auf.
insertCity(
"Punxsutawney"
, 6200,
'2008-31-01'
,
[
"phil the groundhog"
], { name :
"Jim Wehrle"
}
)
insertCity(
"Portland"
, 582000,
'2007-20-09'
,
[
"beer"
,
"food"
], { name :
"Sam Adams"
, party :
"D"
}
)
Wir haben nun drei Städte in unserer Collection, was wir wie vorhin mit
db.towns.find()
überprüfen können.