‏הצגת רשומות עם תוויות MongoDB. הצג את כל הרשומות
‏הצגת רשומות עם תוויות MongoDB. הצג את כל הרשומות

יום חמישי, 27 ביוני 2013

Write and read from mongodb using spring integration

I extend the spring integration demo:Samples (Basic) – MongoDb and add a read capabilities to the MongoDbOutboundAdapterDemo class.

The spring context

  1: <?xml version="1.0" encoding="UTF-8"?>
  2: <beans xmlns="http://www.springframework.org/schema/beans"
  3:   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4:   xmlns:int="http://www.springframework.org/schema/integration"
  5:   xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
  6:   xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  7:         xmlns:beans="http://www.springframework.org/schema/beans"
  8:   xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
  9:     http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
 10:     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 11:                 http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
 12: 
 13:   <mongo:db-factory id="mongoDbFactory" dbname="test"/>
 14: 
 15:   <int-mongodb:outbound-channel-adapter id="deafultAdapter" />
 16:       
 17:   <int-mongodb:outbound-channel-adapter id="adapterWithConverter" mongo-converter="stringConverter"/>
 18: 
 19:   <int-mongodb:inbound-channel-adapter id="simpleInboundAdapter" channel="outputChannel" 
 20:                        query="{address.state : 'CA'}">    
 21:             <int:poller fixed-rate="1000" max-messages-per-poll="1"/>
 22:   </int-mongodb:inbound-channel-adapter>
 23: 
 24:     <int:publish-subscribe-channel id="outputChannel">
 25:     </int:publish-subscribe-channel>
 26:  </beans>
 27: 

 The code


  1: package org.springframework.integration.samples.mongodb.outbound;
  2: 
  3: import org.springframework.context.ApplicationContext;
  4: import org.springframework.context.support.FileSystemXmlApplicationContext;
  5: import org.springframework.integration.Message;
  6: import org.springframework.integration.MessageChannel;
  7: import org.springframework.integration.core.MessageHandler;
  8: import org.springframework.integration.core.SubscribableChannel;
  9: import org.springframework.integration.message.GenericMessage;
 10: import org.springframework.integration.samples.mongodb.domain.Address;
 11: import org.springframework.integration.samples.mongodb.domain.Person;
 12: import org.springframework.integration.samples.mongodb.util.DemoUtils;
 13: /**
 14: *
 15: * @author of the orginal source Oleg Zhurakousky
 16: * @Add reading Zvika Peer 
 17: */
 18: public class MongoDbOutboundAdapterDemo {
 19: 
 20: 
 21:   public static void main(String[] args) throws Exception {
 22:     DemoUtils.prepareMongoFactory(); // will clean up MOngoDb
 23:     new MongoDbOutboundAdapterDemo().runDefaultAdapter();
 24:   }
 25: 
 26:   public void runDefaultAdapter() throws Exception {
 27: 
 28:                 ApplicationContext context  =
 29:         new FileSystemXmlApplicationContext(
 30:                 "C:\\learn\\spring\\integration\\samples\\basic\\mongodb\\src\\main\\java\\org\\springframework\\integration\\samples\\mongodb\\outbound\\mongodb-out-config.xml"
 31:                         );
 32:                         
 33:       MessageChannel messageChannel = context.getBean("deafultAdapter", MessageChannel.class);
 34:       messageChannel.send(new GenericMessage<Person>(this.createPersonA()));
 35:       messageChannel.send(new GenericMessage<Person>(this.createPersonB()));
 36:       messageChannel.send(new GenericMessage<Person>(this.createPersonC()));
 37: 
 38:              SubscribableChannel outputChannel = context.getBean("outputChannel", SubscribableChannel.class);
 39: 
 40:             outputChannel.subscribe(new MessageHandler()
 41:             {
 42:                 @Override
 43:                 public void handleMessage(Message<?> msg)
 44:                 {
 45:                    System.out.println("==> HelloWorldDemo Async message: " + msg.getPayload());
 46:                 }
 47:             });
 48:                 
 49:               Thread.sleep(4000);
 50:             System.out.println("Finished");
 51:         }
 52:   }

The result
Finished
==> HelloWorldDemo Async message: [{ "_id" : { "$oid" : "51c945b3fe3a5df7ab5f7a3d"} , "_class" : "org.springframework.integration.samples.mongodb.domain.Person" , "fname" : "John" , "lname" : "Doe" , "address" : { "street" : "3401 Hillview Ave" , "city" : "Palo Alto" , "zip" : "94304" , "state" : "CA"}}, { "_id" : { "$oid" : "51c945b3fe3a5df7ab5f7a3e"} , "_class" : "org.springframework.integration.samples.mongodb.domain.Person" , "fname" : "Josh" , "lname" : "Doe" , "address" : { "street" : "123 Main st" , "city" : "San Francisco" , "zip" : "94115" , "state" : "CA"}}]


Resources:
http://static.springsource.org/spring-integration/reference/html/mongodb.html

יום שני, 10 ביוני 2013

Pulling sub document from a document

The following code demonstrates the removing of sub document from a document using the pull command .

  1: import pymongo
  2: from datetime import *
  3: client = pymongo.MongoClient("localhost", 27017)
  4: 
  5: db = client['test-database']
  6: 
  7: userCollection = db['userCollection']
  8: 
  9: userCollection.remove ({"Name":"Uzi"} ,safe=True)
 10: 
 11: new_user = {"Name":"Uzi",
 12:     "Age":90,
 13:     "Childs":["fstChild","scnChild"],
 14:     "dateofbirth" : datetime(1970, 10, 25),
 15:     "email" : "loveme42@hotmail.com",
 16:     "RunningNo":nextNo ,
 17:     "Blog address":"http://zvikastechnologiesblog.blogspot.com",
 18:     "BankAcounts":[{"Name":"Leumi" ,"No":"123"},{"Name":"apoalim" ,"No":"456"}]}
 19: 
 20: userCollection.save (new_user)
 21: 
 22: userCollection.update({"Name":"Uzi"},
 23: {"$pull":{"BankAcounts":{"No":{"$ne":"123"}}}}, safe=True)
 24: 
 25: users = db.userCollection.find({"Name":"Uzi"},limit=1)
 26: 
 27: for user in users:
 28:     print ( type ( user))
 29:     print (  user)

And the result:
{'Age': 90, 'dateofbirth': datetime.datetime(1970, 10, 25, 0, 0), 'Blog address': 'http://zvikastechnologiesblog.blogspot.com', '_id': ObjectId('51b5c0fddef1c125e8218723'), 'RunningNo': 175, 'email': 'loveme42@hotmail.com', 'Name': 'Uzi', 'BankAcounts': [{'Name': 'Leumi', 'No': '123'}], 'Childs': ['fstChild', 'scnChild']}

The Mongo Query removes all the sub documents of docuemnts where the sub docuemnt "No" value is not equal to "123"
Note:There is a deiffrent between 123 and “123”.

יום חמישי, 30 במאי 2013

MongoDB Cont

Deleting documents from collection .
The following code deletes all documents that the property RunningNo value is 9

  1: import pymongo
  2: from datetime import *
  3: client = pymongo.MongoClient("localhost", 27017)
  4: #Create of get the DB  
  5: db = client['test-database']
  6: 
  7: print (db.name)
  8: 
  9: #create user collection
 10: 
 11: userCollection = db['userCollection']
 12: 
 13: for nextNo in range (1,10,2):
 14: 
 15:     new_user = {"Name":"zvika",
 16:                 "Age":42,
 17:                 "Childs":["Lior","Gal","Shahf"],
 18:                 "dateofbirth" : datetime(1970, 10, 25),
 19:                 "email" : "loveme42@hotmail.com",
 20:                 "RunningNo":nextNo ,
 21:                 "Blog address":"http://zvikastechnologiesblog.blogspot.com"}
 22: 
 23:     userCollection.save (new_user)
 24: 
 25: userCollection.remove ({"RunningNo":9} ,safe=True)
 26: 
 27: users = db.userCollection.find({"Name":"zvika"},{"RunningNo":1,"email":2},sort=[("RunningNo", pymongo.DESCENDING)] ,limit=24)
 28: 
 29: for user in users:
 30:     print ( type ( user))
 31:     print (  user)

 


using sub documents
  1: new_user = {"Name":"zvika",
  2:     "Age":42,
  3:     "Childs":["Lior","Gal","Shahf"],
  4:     "dateofbirth" : datetime(1970, 10, 25),
  5:     "email" : "loveme42@hotmail.com",
  6:     "RunningNo":nextNo ,
  7:     "Blog address":"http://zvikastechnologiesblog.blogspot.com",
  8:     "BankAcounts":[{"Name":"Leumi" ,"No":"123"},{"Name":"apoalim" ,"No":"456"}]}
  9: 
 10: userCollection.save (new_user)
 11: 
 12: users = db.userCollection.find({"BankAcounts.Name":"Leumi"},limit=1)

Note: the code above returns that all  documents that match the query

Setting anew Property
  1: db.userCollection.update({"BankAcounts.Name":"Leumi"},
  2: {"$set":{"NewField":"NewFieldValue"}}, safe=True)

The Result:
{'email': 'loveme42@hotmail.com', 'Name': 'zvika', 'BankAcounts': [{'No': '123', 'Name': 'Leumi'}, {'No': '456', 'Name': 'apoalim'}], 'Childs': ['Lior', 'Gal', 'Shahf'], 'dateofbirth': datetime.datetime(1970, 10, 25, 0, 0), 'Blog address': 'http://zvikastechnologiesblog.blogspot.com', 'RunningNo': 175, 'NewField': 'NewFieldValue', '_id': ObjectId('51a4bcb4def1c124d490d385'), 'Age': 42}


Insert new property to the collection

  1: db.userCollection.update({"BankAcounts.Name":"Leumi"},
  2:     {"$set":{"BankAcounts.$.NewField":"NewFieldValue"}}, safe=True)

note the $ sign is used to represent a collection
The Result:
{'RunningNo': 175, 'BankAcounts': [{'NewField': 'NewFieldValue', 'No': '123', 'Name': 'Leumi'}, {'No': '456', 'Name': 'apoalim'}], '_id': ObjectId('51a4bd67def1c1143c53f8a4'), 'email': 'loveme42@hotmail.com', 'Name': 'zvika', 'Blog address': 'http://zvikastechnologiesblog.blogspot.com', 'Childs': ['Lior', 'Gal', 'Shahf'], 'Age': 42, 'dateofbirth': datetime.datetime(1970, 10, 25, 0, 0)}

יום שישי, 24 במאי 2013

MongoDB Queries cont

The sample DB for this post :

import pymongo
from datetime import *
client = pymongo.MongoClient("localhost", 27017)
#Create of get the DB 
db = client['test-database']

#create user collection

userCollection = db['userCollection']

for nextNo in range (1,10,2):
   new_user = {"Name":"zvika",
                "Age":42,
                "Childs":["Lior","Gal","Shahf"],
                "dateofbirth" : datetime(1970, 10, 25),
                "email" : "loveme42@hotmail.com",
                "RunningNo":nextNo ,
                "Blog address":"http://zvikastechnologiesblog.blogspot.com"}
   userCollection.save (new_user)

Limiting the returned fields from mondo db results :
Return only the age and email fields :

users = db.userCollection.find({"Name":"zvika"},{"Age":1,"email":2})
for user in users:
    print ( type ( user))
    print (  user)
returns:
<class 'dict'>
{'_id': ObjectId('519ddd10def1c122d4074eef'), 'email': 'loveme42@hotmail.com', 'Age': 42}
Add sorting users = db.userCollection.find({"Name":"zvika"},{"RunningNo":1,"email":2},sort=[("RunningNo", pymongo.DESCENDING)])

Limiting the resutls set
users = db.userCollection.find({"Name":"zvika"},{"RunningNo":1,"email":2},sort=[("RunningNo", pymongo.DESCENDING)]).limit(1)
Note:the result set limitation in this case is relevant to the sorted field in this case if there is a several documents with the same sorted value than all of them will return.
for an example after running the sample data five times the result of the query will be :
<class 'dict'>
{'email':
'loveme42@hotmail.com', '_id': ObjectId('519de196def1c115a050470f'), 'RunningNo': 9}
<class 'dict'>
{'email':
'loveme42@hotmail.com', '_id': ObjectId('519de196def1c115a050470f'), 'RunningNo': 9}
<class 'dict'>
{'email':
'loveme42@hotmail.com', '_id': ObjectId('519de196def1c115a050470f'), 'RunningNo': 9}
<class 'dict'>
{'email':
'loveme42@hotmail.com', '_id': ObjectId('519de196def1c115a050470f'), 'RunningNo': 9}
<class 'dict'>
{'email':
'loveme42@hotmail.com', '_id': ObjectId('519de196def1c115a050470f'), 'RunningNo': 9}
The same result will be applied to limit 1..5 only if the limit will be greater then 6 the next batch will be returned.

The limit parse could be write although in the following syntex:

users = db.userCollection.find({"Name":"zvika"},{"RunningNo":1,"email":2},sort=[("RunningNo", pymongo.DESCENDING)] ,limit=24)

MongoDB simple sample

If the mongod.bat failed to start due to wrong DB path Start the mongodb: mongod -dbpath c:\data where c:\data is an valid and exist path.
I found that using  pymongo driver in order to interact with mongodb is very powerful tool

Simple sample: import pymongo
from datetime import *
client = pymongo.MongoClient("localhost", 27017)
#Create of get the DB 
db = client['test-database']
print (db.name)

#create user collection
userCollection = db['userCollection']

new_user = {"Name":"zvika",
            "Age":42,
            "Childs":["Lior","Gal","Shahf"],
            "dateofbirth" : datetime(1970, 10, 25),
            "email" : "loveme42@hotmail.com"}

userCollection.save (new_user)

users = db.userCollection.find({"Name":"zvika"})
for user in users:
    print ( type ( user))
    print (user.get("Childs"))
The sample  creates  a connection to the local mongo db server .
Gets a reference to a test-client DB  if not exists create it.
Get a reference to the user-collection  if not exits create it.
Add a simple documents to the collection .
Query for the document with the criteria name = zvika
Note the return value is  a list  of dictionaries containing the  query returned documents.