FAQ
I am trying to move a project to the open source world (PHP).
Before I saw the light I was using mongodb with microsoft.net.
BTW: I just finished the mongodb DBA course. I greatly recommend it.

I have a small piece of code that I wrote in .net and want to see if i can
reproduce it in php.

string jsonresult = "";
string connectionString;
MongoDB.Driver.MongoDatabase db;
MongoClient client;
MongoDB.Driver.MongoServer server;
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var db = server.GetDatabase("mydb"); // WriteConcern defaulted to
Acknowledged

var rp = "listcustomer";
// now from here down, i don't know the equivalents in php

MongoDB.Driver.MongoCollection<BsonDocument> collection =
db.GetCollection<BsonDocument>("customers");

// parms is a bson document, but you can assume that in php, it is
an associative array

switch (rp)
{
case "listcustomers":
{
var query = new QueryDocument();
string [] fields = {"_id", "name"};
var cursor =
collection.FindAs<BsonDocument>(query).SetFields(fields);
jsonresult = cursor.ToJson();
break;
}
case "getcustomer":
{
var oid =
MongoDB.Bson.ObjectId.Parse(parms["_id"].ToString());
var query = new QueryDocument("_id", oid);
var cursor = collection.FindOne(query);
jsonresult = cursor.ToJson();
break;
}

case "setcustomer":
{
string _id = parms["_id"].ToString();
if (_id == "")
{
var oid = ObjectId.GenerateNewId();
parms["_id"] = oid;
collection.Save(parms);
}
else
{
var oid =
MongoDB.Bson.ObjectId.Parse(parms["_id"].ToString());
parms["_id"] = oid;
collection.Save(parms);
}
jsonresult = "{\"_id\" : \"" + parms["_id"].ToString()
+ "\"}";
break;
}
case "deletecustomer":
{
var oid =
MongoDB.Bson.ObjectId.Parse(parms["_id"].ToString());
var query = new QueryDocument("_id", oid);
collection.Remove(query);
jsonresult = "{\"_id\" : \"" + parms["_id"].ToString()
+ "\"}";
break;
}
}
Response.Write(jsonresult);

Help is greatly appreciated


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user+unsubscribe@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Search Discussions

  • Jeremy Mikola at Jan 18, 2013 at 5:04 pm
    See: https://gist.github.com/4566112

    This uses the short-array syntax from PHP 5.4+, but is otherwise very
    simple to interpret in conjunction with the PHP driver's API documentation<http://php.net/mongo>
    .
    On Saturday, January 5, 2013 3:10:43 PM UTC-5, gd52 wrote:

    I am trying to move a project to the open source world (PHP).
    Before I saw the light I was using mongodb with microsoft.net.
    BTW: I just finished the mongodb DBA course. I greatly recommend it.

    I have a small piece of code that I wrote in .net and want to see if i can
    reproduce it in php.

    string jsonresult = "";
    string connectionString;
    MongoDB.Driver.MongoDatabase db;
    MongoClient client;
    MongoDB.Driver.MongoServer server;
    var connectionString = "mongodb://localhost";
    var client = new MongoClient(connectionString);
    var server = client.GetServer();
    var db = server.GetDatabase("mydb"); // WriteConcern defaulted to
    Acknowledged

    var rp = "listcustomer";
    // now from here down, i don't know the equivalents in php

    MongoDB.Driver.MongoCollection<BsonDocument> collection =
    db.GetCollection<BsonDocument>("customers");

    // parms is a bson document, but you can assume that in php, it is
    an associative array

    switch (rp)
    {
    case "listcustomers":
    {
    var query = new QueryDocument();
    string [] fields = {"_id", "name"};
    var cursor =
    collection.FindAs<BsonDocument>(query).SetFields(fields);
    jsonresult = cursor.ToJson();
    break;
    }
    case "getcustomer":
    {
    var oid =
    MongoDB.Bson.ObjectId.Parse(parms["_id"].ToString());
    var query = new QueryDocument("_id", oid);
    var cursor = collection.FindOne(query);
    jsonresult = cursor.ToJson();
    break;
    }

    case "setcustomer":
    {
    string _id = parms["_id"].ToString();
    if (_id == "")
    {
    var oid = ObjectId.GenerateNewId();
    parms["_id"] = oid;
    collection.Save(parms);
    }
    else
    {
    var oid =
    MongoDB.Bson.ObjectId.Parse(parms["_id"].ToString());
    parms["_id"] = oid;
    collection.Save(parms);
    }
    jsonresult = "{\"_id\" : \"" + parms["_id"].ToString()
    + "\"}";
    break;
    }
    case "deletecustomer":
    {
    var oid =
    MongoDB.Bson.ObjectId.Parse(parms["_id"].ToString());
    var query = new QueryDocument("_id", oid);
    collection.Remove(query);
    jsonresult = "{\"_id\" : \"" + parms["_id"].ToString()
    + "\"}";
    break;
    }
    }
    Response.Write(jsonresult);

    Help is greatly appreciated

    --
    You received this message because you are subscribed to the Google
    Groups "mongodb-user" group.
    To post to this group, send email to mongodb-user@googlegroups.com
    To unsubscribe from this group, send email to
    mongodb-user+unsubscribe@googlegroups.com
    See also the IRC channel -- freenode.net#mongodb

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupmongodb-user @
categoriesmongodb
postedJan 5, '13 at 8:16p
activeJan 18, '13 at 5:04p
posts2
users2
websitemongodb.org
irc#mongodb

2 users in discussion

Gd52: 1 post Jeremy Mikola: 1 post

People

Translate

site design / logo © 2022 Grokbase