mongodb distinct去重

  MongoDB的destinct命令是获取特定字段中不同值列表。该命令适用于普通字段,数组字段和数组内嵌文档.

mongodb的distinct的语句:

db.users.distinct(‘last_name‘)

等同于 SQL 语句:

select DISTINCT last_name from users

表示的是根据指定的字段返回不同的记录集。

一个简单的实例:

//

> db.addresses.insert({"zip-code": 10010})

> db.addresses.insert({"zip-code": 10010})

> db.addresses.insert({"zip-code": 99701})

> // shell helper:

> db.addresses.distinct("zip-code");

[ 10010, 99701 ]

> // running as a command manually:

> db.runCommand( { distinct: ‘addresses‘, key: ‘zip-code‘ } )

{ "values" : [ 10010, 99701 ], "ok"

//

> db.comments.save({"user": {"points": 25}})

> db.comments.save({"user": {"points": 31}})

> db.comments.save({"user": {"points": 25}})

> db.comments.distinct("user.points");

[ 25, 31 ]
时间: 2024-08-08 08:28:10

mongodb distinct去重的相关文章

关于Django中的数据库操作API之distinct去重的一个误传

关于Django中的数据库操作API之distinct去重的一个误传 最近在做一个Server的项目,后台框架是Apache mod_wsgi + django.django是一个基于Python的Web开发框架,功能十分强大,至于有多强大,还是读者们自己去体验吧.我在这里要说的一个问题是关于Python的ORM功能的.问题就在django提供的数据库操作API中的distinct()函数,了解SQL语句的读者都应该知道,DISTINCT关键字可以在select操作时去重.django里的这个d

.net Distinct 去重问题分析

问题:.net中的distinct方法对于自定义的类(class model )去重时失效.问题分析: model1: 1 public class TestModel1 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public string Address { get; set; } 6 } model2: public class TestModel2 { protected bool Equa

List<object>进行Distinct()去重

原文:List<object>进行Distinct()去重 有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来.如果list<T>中的T是个自定义对象时直接对集合Distinct是达不到去重的效果.我们需要新定义一个去重的类并继承IEqualityComparer接口重写Equals和GetHashCode方法,如下Demo 1 using System; 2 using System.Collections.Ge

SQLSever 第二堂课,主要学习内容为top查询前多少行,distinct去重,order by排序,group by分组,最重要子查询

create database xuesheng drop database xusheng use xuesheng go create table xueshengxinxi ( code int primary key identity(1,1)not null, name varchar(50) not null, age int not null, sex varchar(50) not null, hight int not null, [weight] int not null,

存储过程系列三:根据表别名方式distinct去重插入

1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ           ( bzj, xkzid,  sqid, jtdz, szsf, szqx, szjd, lxdh, yb, instnum, lastdate, datexc)     select  SYS_GUID(), new_xkz_idIn, a.* from    (select distinct   sqid, jtdz, szsf, szqx, szjd, l

in和exists的区别以及exists和distinct去重的区别?

小编相信大家都知道in和exists的区别:1.运用情况不同sql中in适用于子查询得出的结果集记录较少,主查询中的表较大且又有索引的表,.sql中exist适用于外层的主查询记录较少,子查询中的表大,又有索引的时候. 2.驱动顺序不同IN是先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选.exists是以外层表为驱动表,先被访问. 3.底层原理不同in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询. 但是我昨天看到

单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询

今日内容 表查询 单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询 单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) u

SQLSERVER去除某一列的重复值并显示所有数据\DISTINCT去重\ISNULL()求SUM()\NOT EXISTS的使用

进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: 1 select t.* from [PeopleCount] as t where t.procedureID='8334' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单纯的查询 GX 这一列用 distinct 是没有任何问题的 1 select distinct t.GX from [PeopleCount] as t where t.pr

C# Distinct去重泛型List

List<int>去重 List<string>去重 List<T>去重 1. List<int>去重 List<int> ilist = new List<int>() { 1, 2, 3, 4, 2, 3 }; ilist = ilist.Distinct().ToList(); foreach (var item in ilist) { Console.WriteLine(item); } 2. List<string&g