如何为User-Visit-WebPage行为编写MongoDB查询?

时间:2015-10-02 13:17:44

标签: mongodb database nosql

我想为访问MongoDB中的网页的用户编写查询。用户和网站对象如下所示:

User
{
   Integer _id,
   String Name,
   ....
}

WebPage:
{
  Integer _id,
  String URL,
  Integer CountOfVisitor,
  Visitors:
  [
    {
      Integer Visitor_id, //this field is consistent with the User._id field
      Integer VisitTime
    }
    ....
  ]
}

网页集合的示例是

   {
      {1, "http://stackoverflow.com/",10000, [{1,1},{2,13},{5,32}....}],
      {2, "http://codeproject.com/",5000, [{2,6},{4,23},{12,32}....}],
      .....
   }

场景是当新用户访问网页时,新对象将插入到对应WebPage对象的Vistors数组中,并且CountOfVisitor字段将增加1.当旧用户访问网页时, Visitors数组中对应对象的VisitTime字段将增加1(初始值为1)并且CountOfVisitor字段不会更改。
那么如何编写一个或多个查询来实现这样的行为呢?

0 个答案:

没有答案