代码之家  ›  专栏  ›  技术社区  ›  KhoPhi

填充以下用户mongoose

  •  5
  • KhoPhi  · 技术社区  · 8 年前

    序言 :

    我不知道这是否是正确的做法,并将感谢更好的方式这样做。然而,我想试试,但它不起作用。

    遵循模型 :

    let mongoose = require('mongoose');
    let Schema = mongoose.Schema;
    
    let FollowSchema = new Schema({
      user: {
        type: Schema.Types.ObjectId,
        ref: 'User'
      },
      followers: [{
        type: Schema.Types.ObjectId,
        ref: 'Card'
      }],
      following: [{
        type: Schema.Types.ObjectId,
        ref: 'Card'
      }]
    });
    
    module.exports = mongoose.model('Follow', FollowSchema);
    

    let mongoose = require('mongoose');
    let Schema = mongoose.Schema;
    
    let CardSchema = new Schema({
      title: String,
      content: String,
      createdById: {
        type: Schema.Types.ObjectId,
        ref: 'User'
      },
      createdBy: {
        type: String
      }
    });
    
    module.exports = mongoose.model('Card', CardSchema);
    

    遵循逻辑

    当用户A跟随用户B时,做两件事:

    • 将B的用户id推送到“following”(A在B之后)字段上的用户A文档
    • 在字段“followers”(B后跟A)上将A的用户id推送到用户B文档

      router.post('/follow', utils.loginRequired, function(req, res) {
      const user_id = req.user._id;
      const follow = req.body.follow_id;
      
      let bulk = Follow.collection.initializeUnorderedBulkOp();
      
      bulk.find({ 'user': Types.ObjectId(user_id) }).upsert().updateOne({
          $addToSet: {
              following: Types.ObjectId(follow)
          }
      });
      
      bulk.find({ 'user': Types.ObjectId(follow) }).upsert().updateOne({
          $addToSet: {
              followers: Types.ObjectId(user_id)
          }
      })
      
      bulk.execute(function(err, doc) {
          if (err) {
              return res.json({
                  'state': false,
                  'msg': err
              })
          }
          res.json({
              'state': true,
              'msg': 'Followed'
          })
      })
      

      })

    实际DB值

    > db.follows.find().pretty()
    {
        "_id" : ObjectId("59e3e27dace1f14e0a70862d"),
        "user" : ObjectId("59e2194177cae833894c9956"),
        "following" : [
            ObjectId("59e3e618ace1f14e0a708713")
        ]
    }
    {
        "_id" : ObjectId("59e3e27dace1f14e0a70862e"),
        "user" : ObjectId("59e13b2dca5652efc4ca2cf5"),
        "followers" : [
            ObjectId("59e2194177cae833894c9956"),
            ObjectId("59e13b2d27cfed535928c0e7"),
            ObjectId("59e3e617149f0a3f1281e849")
        ]
    }
    {
        "_id" : ObjectId("59e3e71face1f14e0a708770"),
        "user" : ObjectId("59e13b2d27cfed535928c0e7"),
        "following" : [
            ObjectId("59e3e618ace1f14e0a708713"),
            ObjectId("59e13b2dca5652efc4ca2cf5"),
            ObjectId("59e21942ca5652efc4ca30ab")
        ]
    }
    {
        "_id" : ObjectId("59e3e71face1f14e0a708771"),
        "user" : ObjectId("59e3e618ace1f14e0a708713"),
        "followers" : [
            ObjectId("59e13b2d27cfed535928c0e7"),
            ObjectId("59e2194177cae833894c9956")
        ]
    }
    {
        "_id" : ObjectId("59e3e72bace1f14e0a708779"),
        "user" : ObjectId("59e21942ca5652efc4ca30ab"),
        "followers" : [
            ObjectId("59e13b2d27cfed535928c0e7"),
            ObjectId("59e2194177cae833894c9956"),
            ObjectId("59e3e617149f0a3f1281e849")
        ]
    }
    {
        "_id" : ObjectId("59f0eef155ee5a5897e1a66d"),
        "user" : ObjectId("59e3e617149f0a3f1281e849"),
        "following" : [
            ObjectId("59e21942ca5652efc4ca30ab"),
            ObjectId("59e13b2dca5652efc4ca2cf5")
        ]
    }
    > 
    

    查询

    router.get('/follow/list', utils.loginRequired, function(req, res) {
        const user_id = req.user._id;
    
        Follow.findOne({ 'user': Types.ObjectId(user_id) })
          .populate('following')
          .exec(function(err, doc) {
              if (err) {
                  return res.json({
                      'state': false,
                      'msg': err
                  })
              };
    
              console.log(doc.username);
    
              res.json({
                  'state': true,
                  'msg': 'Follow list',
                  'doc': doc
              })
          })
    });
    

    通过上面的查询,从我对猫鼬填充的一点了解来看,我希望从中的每个用户那里获得卡片 following 大堆

    我的理解和期望可能是错误的,但是有了这样一个最终目标,这种填充方法可以吗?或者我是在试图解决人口的聚合任务?

    更新:

    followingCards 数组不包含结果。这是我当前的 Follow 型号:

    > db.follows.find().pretty()
    {
        "_id" : ObjectId("59f24c0555ee5a5897e1b23d"),
        "user" : ObjectId("59f24bda1d048d1edad4bda8"),
        "following" : [
            ObjectId("59f24b3a55ee5a5897e1b1ec"),
            ObjectId("59f24bda55ee5a5897e1b22c")
        ]
    }
    {
        "_id" : ObjectId("59f24c0555ee5a5897e1b23e"),
        "user" : ObjectId("59f24b3a55ee5a5897e1b1ec"),
        "followers" : [
            ObjectId("59f24bda1d048d1edad4bda8")
        ]
    }
    {
        "_id" : ObjectId("59f24c8855ee5a5897e1b292"),
        "user" : ObjectId("59f24bda55ee5a5897e1b22c"),
        "followers" : [
            ObjectId("59f24bda1d048d1edad4bda8")
        ]
    }
    >
    

    Card 型号:

    > db.cards.find().pretty()
    {
        "_id" : ObjectId("59f24bc01d048d1edad4bda6"),
        "title" : "A day or two with Hubtel's HTTP API",
        "content" : "a day or two",
        "external" : "",
        "slug" : "a-day-or-two-with-hubtels-http-api-df77056d",
        "createdBy" : "seanmavley",
        "createdById" : ObjectId("59f24b391d048d1edad4bda5"),
        "createdAt" : ISODate("2017-10-26T20:55:28.293Z"),
        "__v" : 0
    }
    {
        "_id" : ObjectId("59f24c5f1d048d1edad4bda9"),
        "title" : "US couple stole goods worth $1.2m from Amazon",
        "content" : "for what",
        "external" : "https://bbc.com",
        "slug" : "us-couple-stole-goods-worth-dollar12m-from-amazon-49b0a524",
        "createdBy" : "nkansahrexford",
        "createdById" : ObjectId("59f24bda1d048d1edad4bda8"),
        "createdAt" : ISODate("2017-10-26T20:58:07.793Z"),
        "__v" : 0
    }
    

    通过您的填充虚拟示例(@Veeram),我得到了以下回应:

    {"state":true,"msg":"Follow list","doc":{"_id":"59f24c0555ee5a5897e1b23d","user":"59f24bda1d048d1edad4bda8","following":["59f24b3a55ee5a5897e1b1ec","59f24bda55ee5a5897e1b22c"],"followers":[],"id":"59f24c0555ee5a5897e1b23d","followingCards":[]}}
    

    这个 以下卡片 数组为空。

    使用 $lookup 另一方面,查询只返回 []

    我可能错过了什么?

    3 回复  |  直到 8 年前
        1
  •  2
  •   s7vr    8 年前

    您可以使用虚拟填充或 $lookup 聚合管道中的运算符。

    使用虚拟填充

    FollowSchema.virtual('followingCards', {
        ref: 'Card',
        localField: 'following',
        foreignField: 'createdById'
    });
    
    Follow.findOne({ 
       'user': Types.ObjectId(user_id) })
       .populate('followingCards')
       .exec(function(err, doc) {      
          console.log(JSON.stringify(doc));
    });
    

    使用 聚合

    Follow.aggregate([
      {
        "$match": {
          "user": Types.ObjectId(user_id) 
        }
      },
      {
        "$lookup": {
          "from": "cards",
          "localField": "following",
          "foreignField": "createdById",
          "as": "followingCards"
        }
      }
    ]).exec(function (err, doc) {
         console.log(JSON.stringify(doc));
    })
    
        2
  •  0
  •   vijay kumar    8 年前
    var mongoose = require('mongoose'), Schema = mongoose.Schema
    
    var eventSchema = Schema({
    title     : String,
    location  : String,
    startDate : Date,
    endDate   : Date
    });
    
    var personSchema = Schema({
    firstname: String,
    lastname: String,
    email: String,
    dob: Date,
    city: String,
    eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }]
    });
    
    var Event  = mongoose.model('Event', eventSchema);
    var Person = mongoose.model('Person', personSchema);
    

    要显示如何使用填充,请首先创建一个person对象,

       aaron = new Person({firstname: 'Aaron'}) and an event object, 
       event1 = new Event({title: 'Hackathon', location: 'foo'}):
    
       aaron.eventsAttended.push(event1);
       aaron.save(callback); 
    

    然后,在进行查询时,可以像这样填充引用:

     Person
    .findOne({ firstname: 'Aaron' })
    .populate('eventsAttended') .exec(function(err, person) {
    if (err) return handleError(err);
    console.log(person);
     });
    

    //只有当我们把裁判推给那个人的时候,这才有效。事件处理

        3
  •  0
  •   Batuhan Sahan    8 年前

    const { ObjectID } = require("mongodb");
    // import Follow and Activity(Card) schema
    
    const userId = req.tokenData.userId; // edit this too...
    Follow.aggregate([
      {
        $match: {
          user: ObjectID(userId)
        }
      }
    ])
    .then(data => {
      // console.log(data)
      var dataUsers = data[0].following.map(function(item) {
        return item._id;
      });
      // console.log(dataUsers)
      Activity.find(
        { createdById: { $in: dataUsers } },
        {
          _id: 1,
          title: 1,
          content: 1,
          createdBy: 1,
          creatorAvatar: 1,
          activityType: 1,
          createdAt: 1
        }
      )
        // .sort({createdAt:-1)
        .then(posts => res.send({ posts }));
    });