如何创建从MongoDB中收集的JSON饲料 [英] How Do I Create a JSON Feed from a MongoDB Collection

查看:79
本文介绍了如何创建从MongoDB中收集的JSON饲料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个CMS为我的客户与他的照片的工作,并在他的网站上出售它们。对于CMS结束,以及前端,这两者将全部AJAX,它会是很好的得到一个JSON提要设置,这样我就可以使用相同的饲料产生新的页面和意见与JS

所以这个例子料将有一个像{[名字:一个照片,说明:Lorem存有...'],[...]},然后与jQuery或JS我可以创建一个表中的所有他照片,网页等我怎样才能设置此为我自己?

我是不是应该创建,获取从MongoDB的所有数据放在公司它在一个数组比数组转换成JSON?PHP文件

解决方案

  $光标= $这个 - >收藏 - >找到($ params)方法;
    $收益率=阵列();
    $ i = 0;
    而($ CURSOR->规则hasNext())
    {

        $返回[$ i] = $ CURSOR-> GETNEXT();
        //键()函数返回的记录_id
        $返回[$ i ++] ['_ ID'] = $ CURSOR->键();
    }
    返回json_en code($返程);
 

这就是我如何返回JSON frrom蒙戈。

I'm creating a CMS for my client to work with his photographs and to sell them on his site. For the CMS end as well as the front end, which both will be all AJAX, it'd be nice to get a JSON feed setup so that I can just use the same feed to generate new "pages" and "views" with JS.

So this example feed would have like {[name:'A Photo',description:'lorem ipsum...'],[...]} and then with jQuery or JS i can create a table of all his photographs, pages, etc. How can I set this up for myself?

Should I just create a PHP file that gets all the data from the MongoDB put's it in an array than convert the array to JSON?

解决方案

    $cursor = $this->collection->find($params);
    $return = array();
    $i=0;
    while( $cursor->hasNext() )
    {

        $return[$i] = $cursor->getNext();
        // key() function returns the records '_id'
        $return[$i++]['_id'] = $cursor->key();
    }
    return json_encode($return);

That is how I return JSON frrom Mongo.

这篇关于如何创建从MongoDB中收集的JSON饲料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆