querying

MongoDB: Error executing stored JavaScript function

可紊 提交于 2019-12-04 07:32:30
When I run below stored JavaScript function I get errors: > db.system.js.save({_id:"last_n_users", value: function(n){return db.users.find().sort({created_at:-1}).limit(n)}}) > db.eval("last_n_users(10)") Here is the errors: { "value" : "DBQuery: store.users -> undefined" } Why? Please help me? The find() function returns a cursor, which can't be returned from JavaScript. The suggested workaround is to use toArray() to get an array return value. Example ... before : > use admin switched to db admin > db.system.js.save( { _id : "foo", value: function(n){return db.system.indexes.find().limit(n)}

SQLAlchemy: several counts in one query

无人久伴 提交于 2019-12-04 02:24:45
I am having hard time optimizing my SQLAlchemy queries. My SQL knowledge is very basic, and I just can't get the stuff I need from the SQLAlchemy docs. Suppose the following very basic one-to-many relationship: class Parent(Base): __tablename__ = "parents" id = Column(Integer, primary_key = True) children = relationship("Child", backref = "parent") class Child(Base): __tablename__ = "children" id = Column(Integer, primary_key = True) parent_id = Column(Integer, ForeignKey("parents.id")) naughty = Column(Boolean) How could I: Query tuples of (Parent, count_of_naughty_children, count_of_all

Connecting to two databases Mongoid

心已入冬 提交于 2019-12-03 18:53:14
问题 I have two databases that I have to use in my application. I have the following in my mongoid.yml : development: # Configure available database sessions. (required) sessions: # Defines the default session. (required) default: # Defines the name of the default database that Mongoid can connect to. # (required). database: db_development username: myusername password: mypassword # Provides the hosts the default session can connect to. Must be an array # of host:port pairs. (required) hosts: -

Querying SQL table with different values in same column with same ID

夙愿已清 提交于 2019-12-02 23:14:46
问题 I have an SQL Server 2012 table with ID, First Name and Last name . The ID is unique per person but due to an error in the historical feed, different people were assigned the same id. ------------------------------ ID FirstName LastName ------------------------------ 1 ABC M 1 ABC M 1 ABC M 1 ABC N 2 BCD S 3 CDE T 4 DEF T 4 DEG T In this case, the people with ID’s 1 are different (their last name is clearly different) but they have the same ID . How do I query and get the result? The table in

Querying SQL table with different values in same column with same ID

为君一笑 提交于 2019-12-02 13:22:53
I have an SQL Server 2012 table with ID, First Name and Last name . The ID is unique per person but due to an error in the historical feed, different people were assigned the same id. ------------------------------ ID FirstName LastName ------------------------------ 1 ABC M 1 ABC M 1 ABC M 1 ABC N 2 BCD S 3 CDE T 4 DEF T 4 DEG T In this case, the people with ID’s 1 are different (their last name is clearly different) but they have the same ID . How do I query and get the result? The table in this case has millions of rows. If it was a smaller table, I would probably have queried all ID’s with

Connecting to two databases Mongoid

旧时模样 提交于 2019-11-30 02:28:45
I have two databases that I have to use in my application. I have the following in my mongoid.yml : development: # Configure available database sessions. (required) sessions: # Defines the default session. (required) default: # Defines the name of the default database that Mongoid can connect to. # (required). database: db_development username: myusername password: mypassword # Provides the hosts the default session can connect to. Must be an array # of host:port pairs. (required) hosts: - myserverip:27017 databases: secondary: database: db2_development username: myusername password:

MongoDB/PyMongo: Querying multiple criteria - unexpected results

谁说我不能喝 提交于 2019-11-29 14:29:25
问题 I have a collection where some of the objects feature an key foo . I now try to query for all objects that indeed have this key but not with the specific value bar . For this I use the following query: collection.find({'foo': {'$exists': True}, 'foo': {'$ne': 'bar'}}) I thought that both criteria are connected via a logical AND. However, I get also objects that don't feature key foo . In fact, I get the same result when I just use the query collection.find({'foo': {'$ne': 'bar'}}) On the

Eager loading child collection with NHibernate

限于喜欢 提交于 2019-11-29 05:28:32
I want to load root entities and eager load all it's child collection and aggregate members. Have been trying to use the SetFetchMode in FluentNHibernate, but I am getting duplicates in one of the child collection since I have a depth of 3 levels. DistinctRootEntityResultTransformer unfortunately only removes the root duplications. return Session.CreateInvoiceBaseCriteria(query, archived) .AddOrder(new Order(query.Order, query.OrderType == OrderType.ASC)) .SetFetchMode("States", FetchMode.Eager) .SetFetchMode("Attestations", FetchMode.Eager) .SetFetchMode("AttestationRequests", FetchMode.Eager

Querying array elements with Mongo

左心房为你撑大大i 提交于 2019-11-28 22:43:18
How can I query the smoothies that have apple in them? (below is a collection with 3 documents) _id => 1 name => 'best smoothie' ingredients => Array ( [0] => apple [1] => raspberry [2] => orange [3] => banana ) _id => 2 name => 'summer smoothie' ingredients => Array ( [0] => lemon [1] => mint ) _id => 3 name => 'yogurt smoothie' ingredients => Array ( [0] => apple [1] => blueberry ) If you simply run the below query MongoDB is smart enough to figure out what you're trying to do. { ingredients: "apple" } Mongo will see that ingredients is a list and only return documents that contain "apple"

PHP and MySQL: Order by most recent date and limit 10

故事扮演 提交于 2019-11-28 01:50:26
I am building a notes system on my site and I've got to the stage where users can post notes into the MySQL database using PHP and then PHP prints them out on a page. However, when they print/echo out, the oldest one appears first but I want the most recent first. I also want them to be limited to 10, so only 10 appear on the page. Here is my PHP code, your help will be much appreciated: // initialize some variables $notedisplaylist = ""; $myObject = ""; $result = mysql_query("SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time"); while($row = mysql_fetch_array($result)){ $note