2012-07-08 2 views
4

에서 MongoDB의 코드를 작성하는 방법 :이 내가 시도 원래 코드 델파이

obj = { 
    sentence: "this is a sentece", 
    tags: [ "some", "indexing", "words"]  
} 

findOne({tags: "words"}).name); 

내가 델파이 와 I를 위해 MongoDB를의 래퍼로 TMongWire을 사용 쓴 :

//var 
// d:IBSONDocument; 
d:=BSON([ 
    'id',mongoObjectID, 
    'sentence', 'this is a sentece', 
    'tags','["some", "indexing", "words"]' 
]); 
FMongoWire.Insert(theCollection,d); 

송가는 위의 작업


을하지만 난 '태그'로 쿼리 할 때, 나를 위해 작동하지 않을 것

//var 
//q:TMongoWireQuery; 
//qb:IBSONDocument 
qb:=BSON(['tags', '"words"']); //*** 
q:=TMongoWireQuery.Create(FMongoWire); 
q.Query(mwx2Collection, qb); //*** 

어떻게 * 별표 두 줄 쓰기합니까?

답변

6

오류가 쿼리에서 필드 생성의 비트가 아닙니다.

작성한대로 tags 필드를 문자열 배열이 아닌 문자열 속성으로 만들었습니다.

d:=BSON([ 
    'id',mongoObjectID, 
    'sentence', 'this is a sentece', 
    'tags',VarArrayOf(['some', 'indexing', 'words']) 
]); 
FMongoWire.Insert(theCollection,d); 

문자열 배열을 생성하려면 VarArrayOf()으로 전화해야합니다.

편집 됨 : 도입 VarArrayOf()

+0

것은 내가 네이티브 델파이'Array' 유형을 전달하려고합니다. 그래도 문제가 해결되지 않으면 언제든지 [TMongoWire on github] (https://github.com/stijnsanders/TMongoWire/issues)를 게시 할 수 있으며 원저자가 응답/필요한 내용을 문서화 한 것입니다. – Stennie

+0

BSArray() 대신 VarArrayOf() .... 나는 명확한 답변에서 멀지 않았다! :) OleVariant는 다소 시간이 많이 걸리고 플랫폼에 따라 다릅니다 ... 기본 Delphi 배열은 더 잘 이해할 수 있습니다. –

3

TMongoWire는 변형 배열, 예를 들어, 같은 배열을 전달할 수 있도록, 자신의 전체 범위에 OleVariant에 사용하려고합니다

FMongoWire.Insert(theCollection,BSON([ 
    'id',mongoObjectID, 
    'sentence', 'this is a sentece', 
    'tags',VarArrayOf(['some', 'indexing', 'words']) 
]); 

을 캐릭터 라인에는 자바 스크립트 표기 구문 분석이 없다, 그래서 쓰기 : VarArrayOf 사용

q.Query(mwx2Collection, BSON(['tags','words']));