0

I am new to mongoDB and its Syntax.

If I am writing same query in plain SQL my query somethings like as below:

Select u.UserId,u.* from Users u where  u.followersCount>u.friendsCount

Below is my sample Json structure,And I need to select (find) those userID which is having more followers than friends on itself.

I am not able to compared self elements with other elements of same collection elements.

{
  "_id": ObjectId("561d6f8986a0ea57e51ec95c"),
  "status": "True",
  "UserId": "1489245878",
  "followers": [
    "1566382441",
    "1155774331"
  ],
  "followersCount": 2,
  "friendsCount": 5,
  "friends": [
    "1135511478",
    "998082481",
    "565321118",
    "848123988",
    "343334562"
  ]
}
Gautam
  • 3,707
  • 5
  • 36
  • 57

1 Answers1

1

db.users.find({ $where: "this.followersCount > this.friendsCount" }, {"UserId":1, "_id":0} )

Replace users with whatever you named your collection.

Pat Needham
  • 5,698
  • 7
  • 43
  • 63
  • db.user_account.find({$where:'this.FollowersCount > this.FriendsCount'} , {"UserId":1, "_id":0} ) Yes I changed to my collection name user_account. It work for me. – Gautam Mar 02 '17 at 19:12