

>show dbs
admin
local
>db.version()
1.4.2
>db.stats()
{
"collections" : 0,
"objects" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"ok" : 1
}
* This source code was highlighted with Source Code Highlighter.
- > db.items.insert({ name: 'eggs', quantity: 10, price: 1.50 })
- > db.items.insert({ name: 'bacon', quantity: 3, price: 3.50 })
- > db.items.insert({ name: 'tomatoes', quantity: 30, price: 0.50 })
* This source code was highlighted with Source Code Highlighter.
- > db.items.find({})
- { "_id" : ObjectId("4bea15293302000000006dcf"), "name" : "eggs", "quantity" : 10, "price" : 1.5 }
- { "_id" : ObjectId("4bea15463302000000006dd0"), "name" : "bacon", "quantity" : 3, "price" : 3.5 }
- { "_id" : ObjectId("4bea15523302000000006dd1"), "name" : "tomatoes", "quantity" : 30, "price" : 0.5
- }
* This source code was highlighted with Source Code Highlighter.
- > db.items.find({quantity: {$gt: 9}, price: {$lt: 1}})
- { "_id" : ObjectId("4bea15523302000000006dd1"), "name" : "tomatoes", "quantity" : 30, "price" : 0.5
- }
-
* This source code was highlighted with Source Code Highlighter.
- <?php
- try {
- // open connection to MongoDB server
- $conn = new Mongo('localhost');
-
- // access database
- $db = $conn->test;
-
- // access collection
- $collection = $db->items;
-
- // execute query
- // retrieve all documents
- $cursor = $collection->find();
-
- // iterate through the result set
- // print each document
- echo $cursor->count() . ' document(s) found. <br/>';
- foreach ($cursor as $obj) {
- echo 'Name: ' . $obj['name'] . '<br/>';
- echo 'Quantity: ' . $obj['quantity'] . '<br/>';
- echo 'Price: ' . $obj['price'] . '<br/>';
- echo '<br/>';
- }
-
- // disconnect from server
- $conn->close();
- } catch (MongoConnectionException $e) {
- die('Error connecting to MongoDB server');
- } catch (MongoException $e) {
- die('Error: ' . $e->getMessage());
- }
- ?>
* This source code was highlighted with Source Code Highlighter.
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// insert a new document
$item = array(
'name' => 'milk',
'quantity' => 10,
'price' => 2.50,
'note' => 'skimmed and extra tasty'
);
$collection->insert($item);
echo 'Inserted document with ID: ' . $item['_id'];
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// remove a document
$criteria = array(
'name' => 'milk',
);
$r = $collection->remove($criteria, array('safe' => true));
echo 'Removed ' . $r['n'] . ' document(s).';
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// remove a document by ID
$criteria = array(
'_id' => new MongoId('4bea96b400f4784c0a070000'),
);
$collection->remove($criteria);
echo 'Removed document with ID: ' . $criteria['_id'];
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// retrieve existing document
$criteria = array(
'name' => 'eggs',
);
$doc = $collection->findOne($criteria);
// update document with new values
// save back to collection
$doc['name'] = 'apples';
$doc['quantity'] = 35;
$doc['note'] = 'green apples taste sooooo good!';
$collection->save($doc);
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// formulate AND query
$criteria = array(
'quantity' => 30,
'price' => 0.5
);
// retrieve only 'name' and 'price' keys
$fields = array('name', 'price');
// execute query
$cursor = $collection->find($criteria, $fields);
// iterate through the result set
// print each document
echo $cursor->count() . ' document(s) found. <br/>';
foreach ($cursor as $obj) {
echo 'Name: ' . $obj['name'] . '<br/>';
echo 'Price: ' . $obj['price'] . '<br/>';
echo '<br/>';
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// formulate complex query
$criteria = array(
'quantity' => array(
'$gt' => 10,
'$lt' => 50
),
'name' => new MongoRegex('/es$/i')
);
// execute query
$cursor = $collection->find($criteria);
// iterate through the result set
// print each document
echo $cursor->count() . ' document(s) found. <br/>';
foreach ($cursor as $obj) {
echo 'Name: ' . $obj['name'] . '<br/>';
echo 'Quantity: ' . $obj['quantity'] . '<br/>';
echo 'Price: ' . $obj['price'] . '<br/>';
echo '<br/>';
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// execute query
// sort by price
// limit to 3 documents
$cursor = $collection->find();
$cursor->sort(array('price' => 1))->limit(3);
// iterate through the result set
// print each document
echo $cursor->count() . ' document(s) found. <br/>';
foreach ($cursor as $obj) {
echo 'Name: ' . $obj['name'] . '<br/>';
echo 'Quantity: ' . $obj['quantity'] . '<br/>';
echo 'Price: ' . $obj['price'] . '<br/>';
echo '<br/>';
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// execute and explain query
$criteria = array(
'quantity' => array(
'$gt' => 10,
'$lt' => 50
),
'name' => new MongoRegex('/es$/i')
);
$cursor = $collection->find($criteria);
$cursor->sort(array('price' => 1))->limit(3);
print_r($cursor->explain());
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// get GridFS files collection
$gridfs = $db->getGridFS();
// store file in collection
$id = $gridfs->storeFile('/tmp/img_2312.jpg');
echo 'Saved file with ID: ' . $id;
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.> show collections
fs.chunks
fs.files
items
system.indexes
> db.fs.files.find()
{ "_id" : ObjectId("4beaa34f00f4784c0a300000"), "filename" : "/tmp/img_2312.jpg", "uploadDate" : "Wed May 12 2010 18:17:11 GMT+0530 (India Standard Time)", "length" : 11618, "chunkSize" : 262144, "md5" : "e66b9a33c7081ae2e4fff4c37f1f756b" }
* This source code was highlighted with Source Code Highlighter.<html>
<head></head>
<body>
<form method="post" enctype="multipart/form-data">
Select file for upload:
<input type="file" name="f" />
<input type="submit" name="submit" />
</form>
<?php
if (isset($_POST['submit'])) {
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// get GridFS files collection
$gridfs = $db->getGridFS();
// check uploaded file
// store uploaded file in collection and display ID
if (is_uploaded_file($_FILES['f']['tmp_name'])) {
$id = $gridfs->storeUpload('f');
echo 'Saved file with ID: ' . $id;
} else {
throw new Exception('Invalid file upload');
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (Exception $e) {
die('Error: ' . $e->getMessage());
}
}
?>
</body>
</html>
* This source code was highlighted with Source Code Highlighter.<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// get GridFS files collection
$grid = $db->getGridFS();
// retrieve file from collection
$file = $grid->findOne(array('_id' => new MongoId('4beaa34f00f4784c0a300000')));
// send headers and file data
header('Content-Type: image/jpeg');
echo $file->getBytes();
exit;
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
* This source code was highlighted with Source Code Highlighter.<?php
class Toy extends Morph_Object {
public function __construct($id = null)
{
parent::__construct($id);
$this->addProperty(new Morph_Property_String('name'))
->addProperty(new Morph_Property_String('colors'))
->addProperty(new Morph_Property_Integer('minAge'))
->addProperty(new Morph_Property_Integer('maxAge'))
->addProperty(new Morph_Property_Enum('gender', null, array('boys', 'girls', 'both')))
->addProperty(new Morph_Property_Float('price'));
}
}
?>
* This source code was highlighted with Source Code Highlighter.<?php
require_once 'Morph.phar';
// initialize MongoDB connection
$mongo = new Mongo('localhost');
// select database for storage
$storage = Morph_Storage::init($mongo->selectDb('test'));
// create object and set properties
$toy = new Toy();
$toy->name = 'Ride Along Fire Engine';
$toy->colors = 'red,yellow';
$toy->minAge = '2';
$toy->maxAge = '4';
$toy->gender = 'both';
$toy->price = 145.99;
// save to database
$storage->save($toy);
echo 'Document saved with ID: ' . $toy->id();
?>
* This source code was highlighted with Source Code Highlighter.<?php
require_once 'Morph.phar';
// initialize MongoDB connection
$mongo = new Mongo('localhost');
// select database for storage
$storage = Morph_Storage::init($mongo->selectDb('test'));
// create object and set properties
$toy = new Toy();
$toy->loadById('5421d0b9fc6217c5bb929baa14a97e08');
$toy->name = 'Jumping Squid';
$toy->colors = 'red,orange,green,blue,yellow';
$toy->minAge = '2';
$toy->maxAge = '10';
$toy->gender = 'boys';
$toy->price = 22.99;
// save to database
$storage->save($toy);
echo 'Document saved with ID: ' . $toy->id();
?>
* This source code was highlighted with Source Code Highlighter.Только зарегистрированные пользователи могут оставлять комментарии. Войдите, пожалуйста.
комментарии (73)