1
私は記事データを持っていて、すべての記事に複数のタグがあります。だから、記事にタグを保存して複製するのではなく、タグ用の別のコレクションを作成しました。 dreferencingにthis gistに従った後、私はpymongo dreference not working
In [98]: tag = db.tags.find_one({'_id': ObjectId('5839644a41e729e3d36ed28f')})
In [99]: tag
Out[99]: {u'_id': ObjectId('5839644a41e729e3d36ed28f'), u'code': u'new'}
In [100]: article = db.articles.insert_one({'name': 'hello', 'heading': 'test 123', 'tag': DBRef(collection = "tags", id = '5839644a41e729e3d36ed28f')})
In [104]: article.inserted_id
Out[104]: ObjectId('58397eaea09e012ed4202c74')
In [105]: a = db.articles.find_one({'_id': ObjectId('58397eaea09e012ed4202c74')})
In [106]: a
Out[106]:
{u'_id': ObjectId('58397eaea09e012ed4202c74'),
u'heading': u'test 123',
u'name': u'hello',
u'tag': DBRef(u'tags', u'5839644a41e729e3d36ed28f')}
In [107]: a['tag']
Out[107]: DBRef(u'tags', u'5839644a41e729e3d36ed28f')
In [110]: tag = db.dereference(a['tag'])
In [112]: print tag
None
を試してみましたが、私は、関連するタグを取得することはできませんよ。どうしましたか?
私はあなたが言ったことを試みました。 'tag = db.dreference(a ['tag'])'を実行すると、エラー 'TypeError:' Collection 'オブジェクトが呼び出せません。そのようなメソッドが存在しないため、 'データベース'オブジェクトで 'dreference'メソッドを呼び出すことに失敗しています。私はそれは 'dreference'メソッドが実際にそこにないからだと思います。 https://github.com/mongodb/mongo-python-driver – Hussain
"e": "dereference"がありません。 –
はい。要点はそれを参照して頼るための唯一の適切な例です。 'pymongo'に良いリソースがありますか?あなたはそれを広範囲に使用したようです。 – Hussain