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
13ded9a3
Commit
13ded9a3
authored
Dec 04, 2014
by
Hannes Magnusson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CRUD API examples
parent
b646280f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
11 deletions
+62
-11
find.php
examples/find.php
+2
-2
write.php
examples/write.php
+60
-9
No files found.
examples/find.php
View file @
13ded9a3
...
...
@@ -7,8 +7,8 @@ $manager = new MongoDB\Manager("mongodb://localhost:27017");
var_dump
(
$manager
);
$collection
=
new
MongoDB\Collection
(
$manager
,
"
phongo_test.functional_cursor_001
"
);
$result
=
$collection
->
find
(
array
(
"
username"
=>
"pacocha.quentin"
),
array
(
"projection"
=>
array
(
"firstN
ame"
=>
1
)));
$collection
=
new
MongoDB\Collection
(
$manager
,
"
crud.examples
"
);
$result
=
$collection
->
find
(
array
(
"
nick"
=>
"bjori"
),
array
(
"projection"
=>
array
(
"n
ame"
=>
1
)));
foreach
(
$result
as
$document
)
{
...
...
examples/write.php
View file @
13ded9a3
...
...
@@ -14,28 +14,79 @@ $hannes = array(
);
$hayley
=
array
(
"name"
=>
"Hayley"
,
"nick"
=>
"
Alien
Ninja"
,
"nick"
=>
"Ninja"
,
"citizen"
=>
"USA"
,
);
$
jonpall
=
array
(
"name"
=>
"Jon Pall
"
,
"nick"
=>
"unknown
"
,
"citizen"
=>
"Iceland
"
,
$
bobby
=
array
(
"name"
=>
"Robert Fischer
"
,
"nick"
=>
"Bobby Fischer
"
,
"citizen"
=>
"USA
"
,
);
try
{
$hannes_id
=
$collection
->
insertOne
(
$hannes
);
$result
=
$collection
->
insertOne
(
$hannes
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
insertOne
(
$hayley
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
insertOne
(
$bobby
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
find
(
array
(
"nick"
=>
"bjori"
),
array
(
"projection"
=>
array
(
"name"
=>
1
)));
echo
"Searching for nick => bjori, should have only one result:
\n
"
;
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
$result
=
$collection
->
deleteOne
(
$document
);
printf
(
"Deleted: %s (out of expected 1)
\n
"
,
$result
->
getNumRemoved
());
$result
=
$collection
->
updateOne
(
array
(
"citizen"
=>
"USA"
),
array
(
'$set'
=>
array
(
"citizen"
=>
"Iceland"
))
);
printf
(
"Updated: %s (out of expected 1)
\n
"
,
$result
->
getNumModified
());
$result
=
$collection
->
find
(
array
(
"citizen"
=>
"Iceland"
),
array
(
"comment"
=>
"Excellent query"
));
echo
"Searching for citizen => Iceland, verify Hayley is now Icelandic
\n
"
;
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
$result
=
$collection
->
deleteOne
(
$document
);
printf
(
"Deleted: %d (out of expected 1)
\n
"
,
$result
->
getNumRemoved
());
}
catch
(
Exception
$e
)
{
echo
$e
->
getMessage
(),
"
\n
"
;
exit
;
}
try
{
$results
=
$collection
->
insertMany
(
array
(
$hayley
,
$jonpall
));
/* These two were removed earlier */
$result
=
$collection
->
insertOne
(
$hannes
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
insertOne
(
$hayley
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
find
();
echo
"Find all docs, should be 3, verify 2x USA citizen, 1 Icelandic
\n
"
;
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
$result
=
$collection
->
updateMany
(
array
(
"citizen"
=>
"USA"
),
array
(
'$set'
=>
array
(
"citizen"
=>
"Iceland"
))
);
printf
(
"Updated: %d (out of expected 2), verify everyone is Icelandic
\n
"
,
$result
->
getNumModified
());
$result
=
$collection
->
find
();
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
$result
=
$collection
->
deleteMany
(
array
(
"citizen"
=>
"Iceland"
));
printf
(
"Deleted: %d (out of expected 3)
\n
"
,
$result
->
getNumRemoved
());
}
catch
(
Exception
$e
)
{
echo
$e
->
getMessage
(),
"
\n
"
;
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