Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
cb3e7f15
Commit
cb3e7f15
authored
Jun 02, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add write method examples to CRUD tutorial
parent
faf8a1d8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
409 additions
and
355 deletions
+409
-355
crud.md
docs/tutorial/crud.md
+409
-1
bootstrap.php
examples/bootstrap.php
+0
-11
bulkwrite.php
examples/bulkwrite.php
+0
-109
write.php
examples/write.php
+0
-234
No files found.
docs/tutorial/crud.md
View file @
cb3e7f15
This diff is collapsed.
Click to expand it.
examples/bootstrap.php
deleted
100644 → 0
View file @
faf8a1d8
<?php
if
(
file_exists
(
__DIR__
.
'/../vendor/autoload.php'
))
{
// Dependencies were installed with Composer and this is the main project
$loader
=
require_once
__DIR__
.
'/../vendor/autoload.php'
;
}
elseif
(
file_exists
(
__DIR__
.
'/../../../../autoload.php'
))
{
// We're installed as a dependency in another project's `vendor` directory
$loader
=
require_once
__DIR__
.
'/../../../../autoload.php'
;
}
else
{
throw
new
Exception
(
'Can\'t find autoload.php. Did you install dependencies with Composer?'
);
}
examples/bulkwrite.php
deleted
100644 → 0
View file @
faf8a1d8
<?php
require_once
__DIR__
.
"/bootstrap.php"
;
$manager
=
new
MongoDB\Driver\Manager
(
"mongodb://localhost:27017"
);
$collection
=
new
MongoDB\Collection
(
$manager
,
"phplib_demo.bulkwrite"
);
function
dumpWriteResults
(
MongoDB\BulkWriteResult
$result
)
{
printf
(
"Inserted %d documents, upserted %d, updated %d, and deleted %d
\n
"
,
$result
->
getInsertedCount
(),
$result
->
getUpsertedCount
(),
$result
->
getModifiedCount
(),
$result
->
getDeletedCount
()
);
if
(
$result
->
getUpsertedCount
())
{
foreach
(
$result
->
getUpsertedIds
()
as
$index
=>
$id
)
{
printf
(
"upsertedId[%d]: %s
\n
"
,
$index
,
$id
);
}
}
}
function
dumpCollection
(
$collection
)
{
printf
(
"Dumping all documents in: %s.%s
\n
"
,
$collection
->
getDatabaseName
(),
$collection
->
getCollectionName
()
);
$n
=
0
;
foreach
(
$collection
->
find
()
as
$document
)
{
var_dump
(
$document
);
$n
++
;
}
printf
(
"Found %d documents
\n
"
,
$n
);
}
$result
=
$collection
->
bulkWrite
([
[
"insertOne"
=>
[
[
"name"
=>
"Hannes Magnusson"
,
"company"
=>
"10gen"
,
]
],
],
[
"insertOne"
=>
[
[
"name"
=>
"Jeremy Mikola"
,
"company"
=>
"10gen"
,
]
],
],
[
"updateMany"
=>
[
[
"company"
=>
"10gen"
],
[
'$set'
=>
[
"company"
=>
"MongoDB"
]],
],
],
[
"updateOne"
=>
[
[
"name"
=>
"Hannes Magnusson"
],
[
'$set'
=>
[
"viking"
=>
true
]],
],
],
]);
dumpWriteResults
(
$result
);
echo
"
\n
"
;
dumpCollection
(
$collection
);
echo
"
\n
"
;
$result
=
$collection
->
bulkWrite
([
[
"deleteOne"
=>
[
[
"company"
=>
"MongoDB"
],
],
],
[
"updateOne"
=>
[
[
"name"
=>
"Hannes Magnusson"
],
[
'$set'
=>
[
"nationality"
=>
"Icelandic"
]],
[
"upsert"
=>
true
],
],
],
[
"deleteMany"
=>
[
[
"nationality"
=>
[
'$ne'
=>
"Icelandic"
]],
],
],
]);
dumpWriteResults
(
$result
);
echo
"
\n
"
;
dumpCollection
(
$collection
);
echo
"
\n
"
;
$result
=
$collection
->
bulkWrite
([
[
"deleteMany"
=>
[
[],
],
],
]);
dumpWriteResults
(
$result
);
echo
"
\n
"
;
dumpCollection
(
$collection
);
examples/write.php
deleted
100644 → 0
View file @
faf8a1d8
<?php
require_once
__DIR__
.
"/bootstrap.php"
;
$manager
=
new
MongoDB\Driver\Manager
(
"mongodb://localhost:27017"
);
$collection
=
new
MongoDB\Collection
(
$manager
,
"phplib_demo.write"
);
$hannes
=
[
"name"
=>
"Hannes"
,
"nick"
=>
"bjori"
,
"citizen"
=>
"Iceland"
,
];
$hayley
=
[
"name"
=>
"Bayley"
,
"nick"
=>
"Ninja"
,
"citizen"
=>
"USA"
,
];
$bobby
=
[
"name"
=>
"Robert Fischer"
,
"nick"
=>
"Bobby Fischer"
,
"citizen"
=>
"USA"
,
];
$kasparov
=
[
"name"
=>
"Garry Kimovich Kasparov"
,
"nick"
=>
"Kasparov"
,
"citizen"
=>
"Russia"
,
];
$spassky
=
[
"name"
=>
"Boris Vasilievich Spassky"
,
"nick"
=>
"Spassky"
,
"citizen"
=>
"France"
,
];
try
{
$result
=
$collection
->
insertOne
(
$hannes
);
printf
(
"Inserted _id: %s
\n\n
"
,
$result
->
getInsertedId
());
$result
=
$collection
->
insertOne
(
$hayley
);
printf
(
"Inserted _id: %s
\n\n
"
,
$result
->
getInsertedId
());
$result
=
$collection
->
insertOne
(
$bobby
);
printf
(
"Inserted _id: %s
\n\n
"
,
$result
->
getInsertedId
());
$count
=
$collection
->
count
([
"nick"
=>
"bjori"
]);
printf
(
"Searching for nick => bjori, should have only one result: %d
\n\n
"
,
$count
);
$result
=
$collection
->
updateOne
(
[
"citizen"
=>
"USA"
],
[
'$set'
=>
[
"citizen"
=>
"Iceland"
]]
);
printf
(
"Updated: %s (out of expected 1)
\n\n
"
,
$result
->
getModifiedCount
());
$cursor
=
$collection
->
find
(
[
"citizen"
=>
"Iceland"
],
[
"comment"
=>
"Excellent query"
]
);
echo
"Searching for citizen => Iceland, verify Bayley is now Icelandic
\n
"
;
foreach
(
$cursor
as
$document
)
{
var_dump
(
$document
);
}
echo
"
\n
"
;
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
try
{
$cursor
=
$collection
->
find
();
echo
"Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic
\n
"
;
foreach
(
$cursor
as
$document
)
{
var_dump
(
$document
);
}
echo
"
\n
"
;
$result
=
$collection
->
distinct
(
"citizen"
);
echo
"Distinct countries:
\n
"
;
var_dump
(
$result
);
echo
"
\n
"
;
echo
"aggregate
\n
"
;
$result
=
$collection
->
aggregate
(
[
[
'$project'
=>
[
"name"
=>
1
,
"_id"
=>
0
]],
],
[
"useCursor"
=>
true
,
"batchSize"
=>
2
]
);
printf
(
"Should be 3 different people
\n
"
);
foreach
(
$result
as
$person
)
{
var_dump
(
$person
);
}
echo
"
\n
"
;
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
try
{
$result
=
$collection
->
updateMany
(
[
"citizen"
=>
"Iceland"
],
[
'$set'
=>
[
"viking"
=>
true
]]
);
printf
(
"Updated: %d (out of expected 2), verify Icelandic people are vikings
\n
"
,
$result
->
getModifiedCount
());
$result
=
$collection
->
find
();
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
echo
"
\n
"
;
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
try
{
$result
=
$collection
->
replaceOne
(
[
"nick"
=>
"Bobby Fischer"
],
[
"name"
=>
"Magnus Carlsen"
,
"nick"
=>
"unknown"
,
"citizen"
=>
"Norway"
]
);
printf
(
"Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus
\n
"
,
$result
->
getModifiedCount
());
$result
=
$collection
->
find
();
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
echo
"
\n
"
;
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
try
{
$result
=
$collection
->
deleteOne
(
$document
);
printf
(
"Deleted: %d (out of expected 1)
\n\n
"
,
$result
->
getDeletedCount
());
$result
=
$collection
->
deleteMany
([
"citizen"
=>
"Iceland"
]);
printf
(
"Deleted: %d (out of expected 2)
\n\n
"
,
$result
->
getDeletedCount
());
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
try
{
echo
"FindOneAndReplace
\n
"
;
$result
=
$collection
->
findOneAndReplace
(
$spassky
,
$kasparov
,
[
"upsert"
=>
true
]
);
echo
"Kasparov
\n
"
;
var_dump
(
$result
);
echo
"
\n
"
;
echo
"Returning the old document where he was Russian
\n
"
;
$result
=
$collection
->
findOneAndUpdate
(
$kasparov
,
[
'$set'
=>
[
"citizen"
=>
"Croatia"
]]
);
var_dump
(
$result
);
echo
"
\n
"
;
echo
"Deleting him, he isn't Croatian just yet
\n
"
;
$result
=
$collection
->
findOneAndDelete
([
"citizen"
=>
"Croatia"
]);
var_dump
(
$result
);
echo
"
\n
"
;
echo
"This should be empty
\n
"
;
$result
=
$collection
->
find
();
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
echo
"
\n
"
;
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
try
{
$result
=
$collection
->
bulkWrite
(
// Required writes param (an array of operations)
[
// Operations identified by single key
[
'insertOne'
=>
[
[
'x'
=>
1
]
],
],
[
'updateMany'
=>
[
[
'x'
=>
1
],
[
'$set'
=>
[
'x'
=>
2
]],
],
],
[
'updateOne'
=>
[
[
'x'
=>
3
],
[
'$set'
=>
[
'x'
=>
4
]],
// Optional params are still permitted
[
'upsert'
=>
true
],
],
],
[
'deleteOne'
=>
[
[
'x'
=>
1
],
],
],
[
'deleteMany'
=>
[
// Required arguments must still be specified
[],
],
],
],
// Optional named params in an associative array
[
'ordered'
=>
false
]
);
printf
(
"insertedCount: %d
\n
"
,
$result
->
getInsertedCount
());
printf
(
"matchedCount: %d
\n
"
,
$result
->
getMatchedCount
());
printf
(
"modifiedCount: %d
\n
"
,
$result
->
getModifiedCount
());
printf
(
"upsertedCount: %d
\n
"
,
$result
->
getUpsertedCount
());
printf
(
"deletedCount: %d
\n
"
,
$result
->
getDeletedCount
());
foreach
(
$result
->
getUpsertedIds
()
as
$index
=>
$id
)
{
printf
(
"upsertedId[%d]: %s
\n
"
,
$index
,
$id
);
}
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment