Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We need some better documentation please #119

Closed
elfeffe opened this issue Feb 12, 2016 · 7 comments
Closed

We need some better documentation please #119

elfeffe opened this issue Feb 12, 2016 · 7 comments

Comments

@elfeffe
Copy link

elfeffe commented Feb 12, 2016

This is not a bug in the code (I hope) but is a problem with the documentation.
I find how to get collections and then do a var_dump, but I can't find any place that helps me to get some field on that object.
I have tried every single way to do this, and it seems that find or findOne doesn't work. Documentation is not good enough and that's very bad for MongoDB, we just can't advance in our projects.

I have this document

{ 
    "_id" : ObjectId("56a22142bd1457041e2c94b2"), 
    "anonymousId" : "7f668198-e985-426e-9232-084c440f7293", 
    "channel" : "client", 
    "context" : {
        "ip" : "1.175.1.14", 
        "library" : {
            "name" : "analytics.js", 
            "version" : "2.11.1"
        }, 
        "page" : {
            "path" : "/", 
            "referrer" : "http://www.google.com/", 
            "search" : "", 
            "title" : "Title", 
            "url" : "http://www.google.com"
        }, 
        "userAgent" : "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
    }, 
    "integrations" : {

    }, 
    "messageId" : "ajs-492650bab68cd4305f8a56a769299ea6", 
    "originalTimestamp" : "2016-01-22T12:32:10.657Z", 
    "projectId" : "V7vqDI8qKo", 
    "receivedAt" : "2016-01-22T12:32:01.832Z", 
    "sentAt" : "2016-01-22T12:32:10.657Z", 
    "timestamp" : "2016-01-22T12:32:01.832Z", 
    "type" : "identify", 
    "userId" : "user@gmail.com", 
    "version" : NumberInt(2)
}

Can somebody tell me how to get userId from that document in PHP?
Also I have noticed that find and findOne returns different objects, different data.
Can somebody tell me how to get userId from one result and from a collection in a foreach?

This is a silly question, but I can't find any documentation that says that. All the examples are for the old driver (mongo, not mongodb), but not for this.
Thank you

@elfeffe
Copy link
Author

elfeffe commented Feb 12, 2016

I found https://github.com/mongolab/mongodb-driver-examples/blob/master/php/php_simple_example.php
Which helps, but sort doesn't work.

There is no documentation over the whole Internet

@elfeffe
Copy link
Author

elfeffe commented Feb 12, 2016

To collaborate with the documentation:
This is how options (sort, limit) works
http://stackoverflow.com/questions/35147845/how-does-sorting-work-in-the-new-mongodb-pecl-extension

It took more than 2 days to find the way. I hope it helps someone.

@jacqueswaller
Copy link

I've run into the same problems where my old method of "just type what I want to do into Google" always explains how to do it with the old driver.

I have found the API to be useful though: http://mongodb.github.io/mongo-php-library/api/namespace-MongoDB.html

In particular the Client and Collection classes. There's no plainly written examples but having written a lot of code with the old driver I was able to figure stuff out. So yes, as you've noticed find and findOne return different results (this was true with the old driver as well). From the documentation, findOne returns an object or null. find returns a MongoDB\Driver\Cursor. The php library is just a wrapper around the low-level MongoDB driver, so for more information on the driver classes (like MongoDB\Driver\Cursor) you'll want to go here http://php.net/manual/en/set.mongodb.php

@elfeffe
Copy link
Author

elfeffe commented Feb 13, 2016

Thank you @jacqueswaller

@p-golovin
Copy link

I have a problem with deserialisation.

Create client with typemap option:

new \MongoDB\Client("mongodb://".implode(",", $config['server']), $opts, ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);

But

                $mongo_list = $log_collection->distinct("context.user");
                var_dump($mongo_list);

returns me array of objects:

array (size=193)
  0 => 
    object(stdClass)[48]
      public 'login' => string '...' (length=4)
  1 => 
    object(stdClass)[50]
      public 'id' => int 527
      public 'name' => string '...' (length=25)
      public 'login' => string '...' (length=4)

@jacqueswaller
Copy link

I don't have the driver installed atm to test so I can't say for sure, but looking at the API I think that
Distinct::execute could stand to have

if (isset($this->options['typeMap'])) {
    $cursor->setTypeMap($this->options['typeMap']);
}

the same way that Find::execute does.

@jmikola
Copy link
Member

jmikola commented Feb 22, 2016

@elfeffe: Glad you got this sorted out. The prose-style documentation still needs a bit of love, so the API documentation that @jacqueswaller suggested is definitely worth looking at. As a general rule, most of the Collection and Database methods use an Operation class, where we've documented all of the options in the constructor. Of course, I still need to port that over to the prose documentation, since users shouldn't be expected to poke around generated API docs for such basic info.


@p-golovin: We can't support a typeMap option for command helpers until PHPC-314 is implemented. The reason being is that type maps are currently limited to root and embedded documents, and since the non-cursor commands (e.g. distinct, findAndModify) return results within a sub-field (e.g. a results array of objects), we have no way to tell the driver to apply the type map starting with those results, which is undoubtedly what users would expect. The PHPLIB tracking ticket for this is PHPLIB-112.

In the future, please open a new issue unless your question is identical to the OP. That will ensure we don't lose track of things and make it easier for other folks to follow conversation or dig up the question/answer down the line.

@jmikola jmikola closed this as completed Feb 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants