Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion where to use ensure_index ????

Received: by 10.58.202.67 with SMTP id kg3mr674508vec.12.1348355892303;
        Sat, 22 Sep 2012 16:18:12 -0700 (PDT)
X-BeenThere: mongodb-user@googlegroups.com
Received: by 10.52.92.107 with SMTP id cl11ls3557293vdb.4.gmail; Sat, 22 Sep
 2012 16:18:01 -0700 (PDT)
Received: by 10.52.70.78 with SMTP id k14mr1618119vdu.7.1348355881513;
        Sat, 22 Sep 2012 16:18:01 -0700 (PDT)
Received: by 10.52.70.78 with SMTP id k14mr1618117vdu.7.1348355881493;
        Sat, 22 Sep 2012 16:18:01 -0700 (PDT)
Return-Path: <sam.mill...@gmail.com>
Received: from mail-vc0-f176.google.com (mail-vc0-f176.google.com [209.85.220.176])
        by gmr-mx.google.com with ESMTPS id bn19si395079vdb.0.2012.09.22.16.18.01
        (version=TLSv1/SSLv3 cipher=OTHER);
        Sat, 22 Sep 2012 16:18:01 -0700 (PDT)
Received-SPF: pass (google.com: domain of sam.mill...@gmail.com designates 209.85.220.176 as permitted sender) client-ip=209.85.220.176;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of sam.mill...@gmail.com designates 209.85.220.176 as permitted sender) smtp.mail=sam.mill...@gmail.com; dkim=pass header...@gmail.com
Received: by vcbgb22 with SMTP id gb22so6591666vcb.21
        for <mongodb-user@googlegroups.com>; Sat, 22 Sep 2012 16:18:01 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:in-reply-to:references:date:message-id:subject:from:to
         :content-type;
        bh=LgENUhsnZxDFENLJ4EJbEvdExmORxsnPbhUFMcvesPA=;
        b=yZQdVZxkbDMusUds/U1y9F3fNF94HOB/SEw/ieLfKQVapV7f8XeuFfhnqvpHa8oOpe
         qvzl4F6dSkPiwABUNKHUnYqDVgqNCgjqD1TnRN9fDNk5yRedHHZwxcDFffbXZC0ciWPp
         QQSrwAtDjHbx7TnadcTzCuIc5ZPTb+GFIZBePyprCu2sbz91n0bjEj5ts04/RKd9HwRp
         kvWuYwd9ydwKOq1D4fxm2VZHIPsXIsMVfB45I0H3Ti/zlfae1qcpjECEflVwXPErpJWS
         S9lMW7RWsL8XrHOAvTGF66VE9GQF7IGZSgsog/1A7gz1t/ezygJWqxBBQIUdFHDm4+qb
         T6bw==
MIME-Version: 1.0
Received: by 10.58.102.48 with SMTP id fl16mr5372947veb.41.1348355881354; Sat,
 22 Sep 2012 16:18:01 -0700 (PDT)
Received: by 10.58.136.70 with HTTP; Sat, 22 Sep 2012 16:18:01 -0700 (PDT)
In-Reply-To: <7dc3c176-25d1-41ec-ac57-5ed99dc89...@s8g2000vbc.googlegroups.com>
References: <66ba1d47-7358-4a00-a2e9-d8e67746f...@o30g2000vbu.googlegroups.com>
	<CALKyTE6k2r-RF6U1XZuEOAyFD=sEJuVF-6ZR47sBy3rQaxs...@mail.gmail.com>
	<f8dec5e0-797d-4e52-80e8-541e7d30f...@d9g2000vbf.googlegroups.com>
	<CALKyTE7h7ntQqyiupqFO6Stuuc0VLZOKAJRYhN2N-otmhbo...@mail.gmail.com>
	<7dc3c176-25d1-41ec-ac57-5ed99dc89...@s8g2000vbc.googlegroups.com>
Date: Sun, 23 Sep 2012 00:18:01 +0100
Message-ID: <CALKyTE5eenOhum6RpYLX6LhDhSO6_xTHRgQ=Zm0ZTkMdqZD...@mail.gmail.com>
Subject: Re: [mongodb-user] Re: where to use ensure_index ????
From: Sam Millman <sam.mill...@gmail.com>
To: mongodb-user@googlegroups.com
Content-Type: multipart/alternative; boundary=047d7b41903156c48504ca5291df

--047d7b41903156c48504ca5291df
Content-Type: text/plain; charset=ISO-8859-1

No No, ensureIndex is the same as creating an index. It does nothing
special, maybe the name of the function is a little odd really and should
be called create_index instead.

Since Mongo has no pre-defined schema (NoSQL) it has to create indexes a
different way to SQL. The way proposed was through client side when the
user decides they need an index on a particular field. This is how
collections and what not are created, when they are used by the client.

This function was called ensureIndex().

So there is only one call needed to ensureIndex() within your app and it
should be a list of fields for that index as the first param and any extra
options (like sparse) for the second param.

There is no work needed by the administrator to keep this index intact,
well not once a month but if something fails in the cluster then yea, some
work will be needed by the admin to possibly remake the index.

I personally have a file of indexes I call everytime my app is loaded.

I hope this clears things up a little,

On 23 September 2012 00:11, aliane abdelouahab <alabdeloua...@gmail.com>wrote:

> so as from i understand, it's a work made by the administrator (from a
> separated interface that a client cant get), so like that, he can do
> it once a month for example?
>
> On 23 sep, 00:05, Sam Millman <sam.mill...@gmail.com> wrote:
> > Not sure since if it worked like that it would recreate the b-tree index
> > for all rows all over again. So I would probably say no.
> >
> > I think you might have the wrong idea of what ensureIndex is. It is much
> > like in SQL where you create an index on a field. An index can only be
> made
> > once (unless it is removed then remade ofc) nad multiple calls to
> > ensureIndex defining the same index will be classed as no-ops (basically
> > ops that dont run).
> >
> > Here is a SO question on how to ue ensureIndex:
> http://stackoverflow.com/questions/5912661/pymongo-mongodb-create-ind...
> > could be helpful hopefully.
> >
> > On 22 September 2012 23:50, aliane abdelouahab <alabdeloua...@gmail.com
> >wrote:
> >
> >
> >
> >
> >
> >
> >
> > > pseudo will search for pseudo in database
> >
> > > pseudo = self.get_argument("pseudo")
> > > pseudo = db.find({"pseudo":pseud})
> > > and the same for commune, and there is other parameters, but because
> > > those one are the searched fields later...
> >
> > > so if i put it under where i tell mongodb to store the user
> > > information, then, everytime one added a new users, the index will be
> > > stored? then it will be not good for write performances?
> >
> > > On 22 sep, 23:43, Sam Millman <sam.mill...@gmail.com> wrote:
> > > > I saw this question. I think you have a error in your code
> personally.
> >
> > > > What do pseudo and commune equal in your app as you go to call the
> > > > ensureIndex() function?
> >
> > > > On 22 September 2012 23:17, aliane abdelouahab <
> alabdeloua...@gmail.com
> > > >wrote:
> >
> > > > > hi, i've asked it on stackoverflow, and no answers, so i've said
> that
> > > > > maybe ensure_index is only done by the admininstrator with the
> shell?
> > > > > and not when the client clic somewhere!
> > > > > here is the original question:
> >
> > > > > How can I add an index in production code? I have found only the
> way
> > > > > to add it as a command and not embedded in a complete code:
> >
> > > > > If you want to add indexing to speed queries:
> >
> > > > >     db.users.ensure_index(the_key)
> >
> > > > > So I tried to add it to a class:
> >
> > > > >     class Registration(BaseHandler):
> > > > >         def post(self):
> > > > >             # do stuff to get user information using the
> > > > > self.get_argument()
> > > > >             user={"all information":informations}
> > > > >             self.db.users.insert(user, w=1)
> > > > >             self.db.users.ensure_index(pseudo, commune)
> >
> > > > > But I get errors like this:
> >
> > > > >     self.db.users.ensure_index(pseudo, commune)
> > > > >           File "build\bdist.win-amd64\egg\pymongo\collection.py",
> line
> > > > > 829, in ensure_index
> > > > >           return self.create_index(key_or_list, cache_for,
> **kwargs)
> > > > >       File "build\bdist.win-amd64\egg\pymongo\collection.py", line
> > > > > 740, in create_index
> > > > >         self.__name, name, cache_for)
> > > > >       File "build\bdist.win-amd64\egg\pymongo\connection.py", line
> > > > > 330, in _cache_index
> > > > >         expire = datetime.timedelta(seconds=cache_for) + now
> > > > >     TypeError: unsupported type for timedelta seconds component:
> > > > > unicode
> >
> > > > > And I guess it will be the same tip when using inserting a sub-
> > > > > document:
> >
> > > > >     self.db.users.update({"email":email}, {"$push":{"produit_up":
> > > > > {"id":id, "namep":namep, "nombre":nombre}}})
> > > > >     self.db.users.ensure_index("product_up.namep") #????
> >
> > > > > any clues?
> >
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > > > Groups "mongodb-user" group.
> > > > > To post to this group, send email to mongodb-user@googlegroups.com
> > > > > To unsubscribe from this group, send email to
> > > > > mongodb-user+unsubscribe@googlegroups.com
> > > > > See also the IRC channel -- freenode.net#mongodb
> >
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "mongodb-user" group.
> > > To post to this group, send email to mongodb-user@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > mongodb-user+unsubscribe@googlegroups.com
> > > See also the IRC channel -- freenode.net#mongodb
>
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongodb-user@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user+unsubscribe@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
>

--047d7b41903156c48504ca5291df
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

No No, ensureIndex is the same as creating an index. It does nothing specia=
l, maybe the name of the function is a little odd really and should be call=
ed create_index instead.<br><br>Since Mongo has no pre-defined schema (NoSQ=
L) it has to create indexes a different way to SQL. The way proposed was th=
rough client side when the user decides they need an index on a particular =
field. This is how collections and what not are created, when they are used=
 by the client.<br>
<br>This function was called ensureIndex().<br><br>So there is only one cal=
l needed to ensureIndex() within your app and it should be a list of fields=
 for that index as the first param and any extra options (like sparse) for =
the second param.<br>
<br>There is no work needed by the administrator to keep this index intact,=
 well not once a month but if something fails in the cluster then yea, some=
 work will be needed by the admin to possibly remake the index.<br><br>
I personally have a file of indexes I call everytime my app is loaded.<br><=
br>I hope this clears things up a little,<br><br><div class=3D"gmail_quote"=
>On 23 September 2012 00:11, aliane abdelouahab <span dir=3D"ltr">&lt;<a hr=
ef=3D"mailto:alabdeloua...@gmail.com" target=3D"_blank">alabdelouahab@gmail=
.com</a>&gt;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">so as from i understand, it&#39;s a work mad=
e by the administrator (from a<br>
separated interface that a client cant get), so like that, he can do<br>
it once a month for example?<br>
<div class=3D"im"><br>
On 23 sep, 00:05, Sam Millman &lt;<a href=3D"mailto:sam.mill...@gmail.com">=
sam.mill...@gmail.com</a>&gt; wrote:<br>
&gt; Not sure since if it worked like that it would recreate the b-tree ind=
ex<br>
&gt; for all rows all over again. So I would probably say no.<br>
&gt;<br>
&gt; I think you might have the wrong idea of what ensureIndex is. It is mu=
ch<br>
&gt; like in SQL where you create an index on a field. An index can only be=
 made<br>
&gt; once (unless it is removed then remade ofc) nad multiple calls to<br>
&gt; ensureIndex defining the same index will be classed as no-ops (basical=
ly<br>
&gt; ops that dont run).<br>
&gt;<br>
</div>&gt; Here is a SO question on how to ue ensureIndex:<a href=3D"http:/=
/stackoverflow.com/questions/5912661/pymongo-mongodb-create-ind." target=3D=
"_blank">http://stackoverflow.com/questions/5912661/pymongo-mongodb-create-=
ind.</a>..<br>

&gt; could be helpful hopefully.<br>
&gt;<br>
&gt; On 22 September 2012 23:50, aliane abdelouahab &lt;<a href=3D"mailto:a=
labdeloua...@gmail.com">alabdeloua...@gmail.com</a>&gt;wrote:<br>
<div class=3D"HOEnZb"><div class=3D"h5">&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; &gt; pseudo will search for pseudo in database<br>
&gt;<br>
&gt; &gt; pseudo =3D self.get_argument(&quot;pseudo&quot;)<br>
&gt; &gt; pseudo =3D db.find({&quot;pseudo&quot;:pseud})<br>
&gt; &gt; and the same for commune, and there is other parameters, but beca=
use<br>
&gt; &gt; those one are the searched fields later...<br>
&gt;<br>
&gt; &gt; so if i put it under where i tell mongodb to store the user<br>
&gt; &gt; information, then, everytime one added a new users, the index wil=
l be<br>
&gt; &gt; stored? then it will be not good for write performances?<br>
&gt;<br>
&gt; &gt; On 22 sep, 23:43, Sam Millman &lt;<a href=3D"mailto:sam.mill...@g=
mail.com">sam.mill...@gmail.com</a>&gt; wrote:<br>
&gt; &gt; &gt; I saw this question. I think you have a error in your code p=
ersonally.<br>
&gt;<br>
&gt; &gt; &gt; What do pseudo and commune equal in your app as you go to ca=
ll the<br>
&gt; &gt; &gt; ensureIndex() function?<br>
&gt;<br>
&gt; &gt; &gt; On 22 September 2012 23:17, aliane abdelouahab &lt;<a href=
=3D"mailto:alabdeloua...@gmail.com">alabdeloua...@gmail.com</a><br>
&gt; &gt; &gt;wrote:<br>
&gt;<br>
&gt; &gt; &gt; &gt; hi, i&#39;ve asked it on stackoverflow, and no answers,=
 so i&#39;ve said that<br>
&gt; &gt; &gt; &gt; maybe ensure_index is only done by the admininstrator w=
ith the shell?<br>
&gt; &gt; &gt; &gt; and not when the client clic somewhere!<br>
&gt; &gt; &gt; &gt; here is the original question:<br>
&gt;<br>
&gt; &gt; &gt; &gt; How can I add an index in production code? I have found=
 only the way<br>
&gt; &gt; &gt; &gt; to add it as a command and not embedded in a complete c=
ode:<br>
&gt;<br>
&gt; &gt; &gt; &gt; If you want to add indexing to speed queries:<br>
&gt;<br>
&gt; &gt; &gt; &gt; =A0 =A0 db.users.ensure_index(the_key)<br>
&gt;<br>
&gt; &gt; &gt; &gt; So I tried to add it to a class:<br>
&gt;<br>
&gt; &gt; &gt; &gt; =A0 =A0 class Registration(BaseHandler):<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 def post(self):<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 # do stuff to get user informat=
ion using the<br>
&gt; &gt; &gt; &gt; self.get_argument()<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 user=3D{&quot;all information&q=
uot;:informations}<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 self.db.users.insert(user, w=3D=
1)<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 self.db.users.ensure_index(pseu=
do, commune)<br>
&gt;<br>
&gt; &gt; &gt; &gt; But I get errors like this:<br>
&gt;<br>
&gt; &gt; &gt; &gt; =A0 =A0 self.db.users.ensure_index(pseudo, commune)<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 File &quot;build\bdist.win-amd64\eg=
g\pymongo\collection.py&quot;, line<br>
&gt; &gt; &gt; &gt; 829, in ensure_index<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 return self.create_index(key_or_lis=
t, cache_for, **kwargs)<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 File &quot;build\bdist.win-amd64\egg\pymong=
o\collection.py&quot;, line<br>
&gt; &gt; &gt; &gt; 740, in create_index<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 self.__name, name, cache_for)<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 File &quot;build\bdist.win-amd64\egg\pymong=
o\connection.py&quot;, line<br>
&gt; &gt; &gt; &gt; 330, in _cache_index<br>
&gt; &gt; &gt; &gt; =A0 =A0 =A0 =A0 expire =3D datetime.timedelta(seconds=
=3Dcache_for) + now<br>
&gt; &gt; &gt; &gt; =A0 =A0 TypeError: unsupported type for timedelta secon=
ds component:<br>
&gt; &gt; &gt; &gt; unicode<br>
&gt;<br>
&gt; &gt; &gt; &gt; And I guess it will be the same tip when using insertin=
g a sub-<br>
&gt; &gt; &gt; &gt; document:<br>
&gt;<br>
&gt; &gt; &gt; &gt; =A0 =A0 self.db.users.update({&quot;email&quot;:email},=
 {&quot;$push&quot;:{&quot;produit_up&quot;:<br>
&gt; &gt; &gt; &gt; {&quot;id&quot;:id, &quot;namep&quot;:namep, &quot;nomb=
re&quot;:nombre}}})<br>
&gt; &gt; &gt; &gt; =A0 =A0 self.db.users.ensure_index(&quot;product_up.nam=
ep&quot;) #????<br>
&gt;<br>
&gt; &gt; &gt; &gt; any clues?<br>
&gt;<br>
&gt; &gt; &gt; &gt; --<br>
&gt; &gt; &gt; &gt; You received this message because you are subscribed to=
 the Google<br>
&gt; &gt; &gt; &gt; Groups &quot;mongodb-user&quot; group.<br>
&gt; &gt; &gt; &gt; To post to this group, send email to <a href=3D"mailto:=
mongodb-user@googlegroups.com">mongodb-user@googlegroups.com</a><br>
&gt; &gt; &gt; &gt; To unsubscribe from this group, send email to<br>
&gt; &gt; &gt; &gt; <a href=3D"mailto:mongodb-user%2Bunsubscribe@googlegrou=
ps.com">mongodb-user+unsubscribe@googlegroups.com</a><br>
&gt; &gt; &gt; &gt; See also the IRC channel -- <a href=3D"http://freenode.=
net#mongodb" target=3D"_blank">freenode.net#mongodb</a><br>
&gt;<br>
&gt; &gt; --<br>
&gt; &gt; You received this message because you are subscribed to the Googl=
e<br>
&gt; &gt; Groups &quot;mongodb-user&quot; group.<br>
&gt; &gt; To post to this group, send email to <a href=3D"mailto:mongodb-us=
er@googlegroups.com">mongodb-user@googlegroups.com</a><br>
&gt; &gt; To unsubscribe from this group, send email to<br>
&gt; &gt; <a href=3D"mailto:mongodb-user%2Bunsubscribe@googlegroups.com">mo=
ngodb-user+unsubscribe@googlegroups.com</a><br>
&gt; &gt; See also the IRC channel -- <a href=3D"http://freenode.net#mongod=
b" target=3D"_blank">freenode.net#mongodb</a><br>
<br>
--<br>
You received this message because you are subscribed to the Google<br>
Groups &quot;mongodb-user&quot; group.<br>
To post to this group, send email to <a href=3D"mailto:mongodb-user@googleg=
roups.com">mongodb-user@googlegroups.com</a><br>
To unsubscribe from this group, send email to<br>
<a href=3D"mailto:mongodb-user%2Bunsubscribe@googlegroups.com">mongodb-user=
+unsubscribe@googlegroups.com</a><br>
See also the IRC channel -- <a href=3D"http://freenode.net#mongodb" target=
=3D"_blank">freenode.net#mongodb</a><br>
</div></div></blockquote></div><br>

--047d7b41903156c48504ca5291df--