---
title: "mongodb使用"
author: "primernd"
date: "2014-05-20T19:18:56Z"
tags: []
categories: ["开源软件"]
slug: "mongodb"
draft: false
lastmod: "2014-05-20T19:18:56Z"
id: 2068791
url: "https://primernd.iteye.com/blog/2068791"
---

1、控制台运行：

db.T\_REPOST\_TWEETS.ensureIndex({"tweetId":1,"serviceId":1},{"unique":true});  
设置唯一索引

2、mongodb监控运行：

{"distinct":"T\_REPOST\_TWEETS","key":"tweet.user.userId","query"{"serviceId":"3530525379450328"}}  
根据serviceID来查询userID是否去重

3、控制台运行：

db.runCommand({"distinct":"T\_REPOST\_TWEETS","key":"tweet.user.userId","query":{"serviceId":"3530525379450328"}}).values.length();

 

根据serviceID来查询userID是否去重